user1158264
user1158264

Reputation: 101

Check jQuery version and load a newer version

I have various web pages that have varying versions of jQuery included on them ranging from 1.4.2 upwards (yes, I know, it's a mess). I want to use the jQuery validation plugin on these pages but it requires jQuery 1.6.4+.

I know I can check the version of jQuery loaded and then load in a newer version if necessary but I'm unsure on how to reference them individually so the pre-existing JavaScript/jQuery code that's on the site can still use $ (as I know this works quite happily and I don't want to break it) and then the new plugin can use something else?

Any help or suggestions on a better way to do this would be greatly appreciated. Thanks.

Upvotes: 0

Views: 340

Answers (2)

Brett Weber
Brett Weber

Reputation: 1907

  • original version of jquery loads (Version1)
    • $ and jQuery belong to version1
  • second version of jquery loads (Verion2)
    • now $ and jQuery belong to Version2, and _$ and _jQuery that belongs to Version1
  • assign the Version2 version to a var ($v2 = jQuery.noConflict(true);)
    • now $ and jQuery belong to Version1, _$ and _jQuery are probably null, and $v2 is Version2

Upvotes: 1

ZorleQ
ZorleQ

Reputation: 1437

Bad idea to do what you are doing.

If you really insist on having different jquery versions, you can always write some sort of a script manager. Basically you specify in your page's "config" what jquery versions are required on this page and the manager will load the appropriate one.

Something like:

// each page content before everything else
Manager.reguire("plugin 1", "1.4.7");
Manager.require("plugin 2", "1.4.4");


// Main layout <head section>
Manager.LoadRequiredVersions();

And the manager would just request the files for you, but don't forget that you might have to do some clever no-conflict stuff with jQuery if you want more than one on a single page. https://stackoverflow.com/a/1566644/486780

If it's one version per page then a manager would be the easiest option.

Upvotes: 2

Related Questions