jQuery: Update jQuery-version on the fly via $.getScript

408 views Asked by At

If you are in an environment that starts out with jQuery 1.7.x, are there any risks to run $.getScript and load the latest version of jQuery (1.8.3)?

Will it overwrite the updated functions correctly or will there be any collisions?

$.getScript('http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js')
3

There are 3 answers

4
Mike Robinson On

It'll probably screw up all your event bindings, much like if you include two versions of jQuery the normal way.

It'll be asynchronous, so all your other scripts will execute before the getScript completes. This will be especially troublesome for your plugins since they're binding themselves to the jQuery.fn namespace. Once the new script is loaded in, it'll recreate the jquery object and wipe out the references to your plugins.

You'll essentially perform a factory reset on the jquery object.

That being said, you could put your initialization code inside the getScript callback, but you'd only be able to use core jQuery and none of your plugins.

$.getScript('http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js', function() {
    console.log("Hi there");
})
0
neouser99 On

You could $.noConflict jquery out of the gate, which should erase window.$, then use jQuery.getScript to load the latest jquery into window.$.

Maybe? this is purely a suggestion, I have never done it before.

jquery noConflict

0
RazorEdge On

Here is how to use noconflict AND getscript to load a newer version of jquery/jqueryui within a contained block in case you are working inside of a context that already has the other versions loaded.

http://www.ipreferjim.com/2011/06/loading-newer-versions-of-jquery-and-jquery-ui-noconflict/