Andy
Andy

Reputation: 19251

how to get multiple versions of jQuery on the same page with one version just for a specific plug-in

I am having issues with using jQuery.noConflict(true)

var jq = $.noConflict(true);

(function(jq) {
    var jq = $;
    console.log($);
})(_jQuery)

But i keep keep getting errors that its undefined. Is something able to help me pass a value to this function so I can use jQuery ?

Edit: I am trying to run a plugin inside an automatic executing function

(function($) {
console.log($); 

   (function($){

   })(jQuery)

})(jq)

The problem is that the function constantly get undefined ?

Upvotes: 0

Views: 932

Answers (1)

jfriend00
jfriend00

Reputation: 707386

I think perhaps you meant to do something like this which removes all jQuery globals, assigns the main jQuery global to your own variable jq, then passes that to a self-executing function which takes an argument named $ so you can then use $ inside of the self executing function as the name for jQuery like this:

var jq = jQuery.noConflict(true);

(function($) {
    console.log($);
})(jq);

If what you're really trying to do is to load multiple versions of jQuery on the same page, you can read this post: Can I use multiple versions of jQuery on the same page?.

Here's the principle in execution:

<!-- load jQuery 1.3.2 -->
<script type="text/javascript" src="http://example.com/jquery-1.3.2.js"></script>
<script type="text/javascript">
var jQuery_1_3_2 = $.noConflict(true);
</script>

<!-- load jQuery 1.8.2 -->
<script type="text/javascript" src="http://example.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
var jQuery_1_8_2 = $.noConflict(true);
</script>

Then, to create an environment for your plugin, you can do this:

(function(jQuery) {
    var $ = jQuery;
    // now both jQuery and $ refer to version jQuery 1.3.2 inside this closure
    // use your plugin that requires 1.3.2 here
})(jQuery_1_3_2);

If this was my project, I'd spend some effort to figure out how to get all my code to use the same version of jQuery because multiple versions of jQuery slows down the loading of the page, consumes more memory and complicates the development and troublshooting. There is nothing good about it.

Upvotes: 2

Related Questions