Question

Say I am running a version of jQuery in no conflict like this:

<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 be run with the no conflict version jQuery191 and included the jQuery object of (jQuery) at the bottom of the script.

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

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

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

Was it helpful?

Solution

getScript will just load the script and add it to the page, it won't affect the script's behavior at all. If the script accesses jQuery by it's global name, then it won't find it (due to the noConflict being called), and won't be able to run correctly.

If you can, I'd suggest including the script before calling noConflict, otherwise you'll have to modify the script to look for jQuery in the place you put it (jQuery191).

OTHER TIPS

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

That means that $.getScript doesn't use that variable, but calling jQuery.getScript works. Because it's an anonymous function, the $ inside the wrapper now reference to the outer $ variable (in this case, the $ in the global object). You can solve this by renaming the first parameter of your wrapper to $:

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

Or by adding this in your wrapper:

var $ = jQuery

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top