0

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 ?

Andy
  • 18,723
  • 12
  • 46
  • 54
  • 2
    What are you actually trying to accomplish because your code doesn't make sense. You remove all jQuery variables from the global space and assign the main jQuery name to `jq`, but then don't use that variable. You try to use `_jQuery` and `$` after that, neither of which should be define. – jfriend00 Oct 14 '12 at 17:54
  • Well when you use `noConflict(true)` they reassign it to `_jQuery` - basically I am trying to use `noConflict` and then use that inside a self-executing function ? – Andy Oct 14 '12 at 17:55
  • As already stated by @jfriend00, you should really be focusing your efforts on making all plugins use the latest (and single) version of jQuery. – Sparky Oct 14 '12 at 19:39

1 Answers1

2

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.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Hey thanks heaps - I am trying to also use a plugin inside this and keeps giving me undefined ? i.e. like `(function($) {console.log($); (function($){})(jQuery)})(jq)` – Andy Oct 14 '12 at 17:58
  • Why are you doing `noConflict(true)` in the first place? The plug-in may be relying on `jQuery` being defined which you are removing. – jfriend00 Oct 14 '12 at 17:59
  • Yeah so how can I fix it for the plugin to work inside the noConflict ? i.e. the problem is that I am loading this on another page where an old version of jQuery is working ? – Andy Oct 14 '12 at 18:00
  • yeah but that question doesn't help me understand how to fix the above problem ? – Andy Oct 14 '12 at 18:03
  • 1
    @Andy - I've expanded my answer using info from that post. This should show you exactly how to do it. Personally, I would endeavor to find fixes for the plug-in to run it with the same version of jQuery as the rest of your page because it's really not desirable to load multiple versions of jQuery (performance, load time, memory consumption, problem troubleshooting, etc...). – jfriend00 Oct 14 '12 at 19:32