Can getcript run imported scripts in jQuery noconflict?

Let's say that I run the jQuery version without conflicts:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><script type="text/javascript">var jQuery191 = $.noConflict(true);</script> 

And I had an external .js file that should run with a non-conflict version of jQuery191 and included a jQuery (jQuery) object at the bottom of the script.

If I included the .js file in getScript() and ran it without a jQuery object without conflict:

 (function (jQuery) { $.getScript("js.js"); })(window.jQuery191) 

Will the script run with jQuery191 or with the original jQuery ? Or this logic is just dumb.

+4
source share
2 answers

getScript simply load the script and add it to the page, it will not affect the script behavior at all. If a script accesses jQuery by its global name, it will not find it (due to a call to noConflict ) and will not be able to work correctly.

If you can, I would suggest that you enable the script before calling noConflict , otherwise you will have to modify the script to look for jQuery where you put it ( jQuery191 ).

+2
source

Inside your wrapper ( (function() { ... })() ) you define the jQuery variabele variable for a copy of the jQuery191 variable in the global scope.

This means that $.getScript does not use this variable, but calls jQuery.getScript . Since this is an anonymous function, $ inside the shell now refers to an external variable $ (in this case $ in a global object). You can solve this by renaming the first parameter of your wrapper to $ :

 (function ($) { $.getScript('foo.js'); })(window.jQuery191); 

Or adding this to your wrapper:

 var $ = jQuery 

Now both $ and jQuery contain a copy of the jQuery191 global scope variable.

0
source

All Articles