imagine the following setting: Page with an old version of jQuery (say 1.5.2) - I have no control over downloading a Javascript file from my servers, which also requires jQuery, but a later version. (e.g. 1.8.3) Now my script is trying to do the following:
var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"); script.onreadystatechange = function() {
The ready function then checks to see if $ correct jQuery version, and if so, it associates this jQuery instance with another variable and returns $ to the original jQuery version via newjQuery = window.jQuery.noConflict(true); .
Now it works fine and dandy, and there is no (external) code execution between downloading the "my" version of jQuery and restoring $ to the "their" version of jQuery - at least in Chrome, Firefox, etc. When this approach does not work, it is Internet Explorer, which for some reason processes at least one more βtickβ of Javascript code, which can work in parallel. This tends to mess up code that is incompatible with the βmyβ version of jQuery and happens to run in 5 ms, where IE has not yet completed the finished event.
Here is an example script: http://jsfiddle.net/w5pPp/3/
In the fiddle, I am testing the current version of jQuery with an accuracy of 10 ms. Only in IE9 sometimes there is a short time when $ refers to "my" jQuery, as stated in the journal "THIS SHOULD NOT HAPPEN."
Now my question is: What would be the best solution for loading the βmyβ version of jQuery into its own variable without any problems when executing the page code in a short amount of time, where it overwrites βitsβ jQuery before calling noConflict?
source share