-2

I just found one website, where are two versions of jQuery included in HTML and both loaded. All scripts on the website are working without problems.

Is there any reason for doing that? Thanks.

<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Legionar
  • 7,472
  • 2
  • 41
  • 70
  • Probably plugins that rely on an old version. – tymeJV Aug 22 '16 at 15:25
  • you should email the webmaster for that site if you want to know why they did that, anything else is speculation and **opinion based** at best, this is **off-topic**. –  Aug 22 '16 at 15:37
  • As @Dekel wrote in his answer, second initialization of jQuery will overwrite the first one... so there is no speculation. – Legionar Aug 22 '16 at 15:38
  • Bad practices, a lazy developer or a stupid "developer" – jherax Aug 22 '16 at 15:41
  • Legionar: When you use complex jQuery plugins, is not recommendable to use more than one version. Compatibility issues are often present. A good code debugging is always good. – Joel Hernandez Aug 22 '16 at 15:26

2 Answers2

3

In this case it is probably just a mistake and there really is no good reason for it. If the intention was to use the second url as a fallback, they should have specified the same version and there are better ways to accomplish this.

The jQuery team does not recommend loading two versions of jQuery as shown on the jQuery.noConflict() page on the jQuery site:

If for some reason two versions of jQuery are loaded (which is not recommended), calling $.noConflict( true ) from the second version will return the globally scoped jQuery variables to those of the first version.

Community
  • 1
  • 1
mhu
  • 17,720
  • 10
  • 62
  • 93
0

There is no reason to do so, and it seems like the programmer was trying to upgrade to version 2, but for some reason it didn't work and he decided to go back to version 1 and forgot to remove the <script> tag of version 2.

The second <script> line will load version 1 of jQuery and will override version 2.

Here is an example:

console.log("jQuery active version is: " + $.fn.jquery)
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Note that it is possible to run two versions of jQuery on the same site, but not the way you wrote in the questions.

More info here.

Community
  • 1
  • 1
Dekel
  • 60,707
  • 10
  • 101
  • 129
  • Thank you, yes, this could be the only one reason. – Legionar Aug 22 '16 at 15:36
  • You know it's possible to run two versions of jQuery at the same time right? See this: http://stackoverflow.com/questions/1566595/can-i-use-multiple-versions-of-jquery-on-the-same-page – DavidG Aug 22 '16 at 15:37
  • 2
    @DavidG, yes, but it requires you to have the `noConflict()` between the two calls (which is **not** the case in the original question). – Dekel Aug 22 '16 at 15:38
  • There's also the case where the first CDN goes offline, that will enable the second jQuery to be run instead. – DavidG Aug 22 '16 at 15:40
  • @Legionar How do you know it's the correct answer if you can't ask the person who wrote that website? That's why you got a downvote I am guessing. – DavidG Aug 22 '16 at 15:45
  • I hope the downvoter will see the comment and add an explanation regarding the downvote on my answer. – Dekel Aug 22 '16 at 15:45
  • @DavidG Because it is really overriding version 2 and so the first load of jQuery making no sense. `console.log("jQuery active version is: " + $.fn.jquery) jQuery active version is: 1.11.1` – Legionar Aug 22 '16 at 15:46
  • @Legionar Well without seeing the website, we cannot really confirm either way. – DavidG Aug 22 '16 at 15:47
  • I run script to see, what is in `$.fn.jquery` and its `1.11.1`. – Legionar Aug 22 '16 at 15:49
  • @Legionar Like Jarrod said above though, we cannot know the motives of that website creator. – DavidG Aug 22 '16 at 15:51
  • 1
    @DavidG, I agree with that, however if you will see that code: `var a = 1; a = 2;` and someone will ask "what is going on there with the variable `a`" - your conclusion will probably be the same as in my answer :) – Dekel Aug 22 '16 at 15:53