encapsulate your functions. This way, within the scope of your encapsulated function, the $ object will always be the same jQuery version, with the same plugins defined.
The moment you go out of scope (as in my example with the default doStuff function for the global object, and for all intents and purposes, you lost the reference to the old jquery object.
If you need plugins for both jquery objects, I suggest reloading the plugin scripts via $ .getScript () after loading the second jquery. This shouldn't cause any additional overhead as the browser will store these files in the cache.
This way you can have multiple versions of jquery, angular, independently of each other on the same page, each in its own scope where it is blissfully unaware of the state of $, jQuery or any other variable :-)
The best part about this is the script load times. Compile your set of libaries as getScript has a callback that will tell you when it's done. When all scripts are done, run the function to execute the rest of the code, walk through the appropriate objects, etc.
This way you always know which object has and can do what :-)
(function($) {
$(document.body).append('<img src="http://weknowyourdreamz.com/images/happy/happy-03.jpg">');
$.getScript("https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js",function() {
doStuff();
});
console.log("JQuery version encapsulated",$.fn.jquery);
})(jQuery)
function doStuff() {
console.log("JQuery version global",$.fn.jquery);
}
img {width:100%;height:100%;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Run code
Tschallacka
source
to share