Thursday, July 2, 2009

jQuery.noConflict – Resolving conflicts with other javascript libraries that use $() function

One of the reasons that make a software popular is its extensions and plugins. jQuery has plenty of plugins that do almost anything you want, from simple button hide to full blown galleries. Plugins let non developers easily add functionality they need to their websites and there are times when one might include more than one javascript library such as prototype.js, YUI or mootools with jQuery. They all use $ as their main function name. Including second library might brake the behavior of the first one. To resolve such cases jQuery introduces .noConflict() method.

When you call .noConflict() jQuery will return $() to it’s previous owner and you will need to use jQuery() instead of shorthand $() function.

.noConflict() usage example (From jQuery Docs site)

<html>
  <head>
  <script src="prototype.js"></script>
  <script src="jquery.js"></script>
  <script>
    jQuery.noConflict();
    // Use jQuery via jQuery(...)
    jQuery(document).ready(function(){
        jQuery("div").hide();
    });
    // Use Prototype with $(...), etc.
    $('someid').hide();
  </script>
  </head>
  <body></body>
</html>

You can also use the following code snippets to still use $() in your code, but with one drawback, you will not have access to your other library’s $() method.

// Method 1
jQuery(document).ready(function($){
    $("div").hide();
});

// Method 2
(function($) {
    /* some code that uses $ */ 
})(jQuery);

TIP:
Don’t forget that you can always assign jQuery to any other variable name to use it as your shorthand: var $_ = jQuery;

15 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. Hi, I was wondering if you could help me with a conflict on my site.

    I have a menu that uses jQuery, and a script (LightView) running prototyp.

    Ive researched the conflict, and tried to understand how to fix it, but I'm pretty much lost.

    I dont know where to properly use .noConflict()

    Could you take a look at mysite and maybe suggest a solution?

    My email is squirrelslax[at]gmail.com
    thank you

    ReplyDelete
  3. This doesn't tell you how to use other libraries.

    ReplyDelete
  4. Old post I know. Just wanted say thanks. All I had to do was open up my jquery file and auto-replace every $( with jquery( and it worked fine with prototype.js on the same page.

    ReplyDelete
  5. @Herbert Weaver

    thank you very much. I just CTRL + F and replace every $ like you did. thanks again :)

    ReplyDelete
  6. I am trying to fix problem on my website, but because of using so many jquery libraries, it takes me a lot of time. Thanks for sharing, I was looking for it for almost half an hours:)!

    ReplyDelete
  7. Thanks so much, you have clearly explained something that has been giving me a headache for the last two hours!

    Works a treat.

    ReplyDelete
  8. Thank you so much for explaining it with good example, i have searched a lot but i did'nt find any thing workable.

    Nice.... :-)

    ReplyDelete
  9. I wonder why method two doesn't work. I even tried using $_ as the parameter and replaced all $ with $_ in my script. Any explanation?

    ReplyDelete
  10. Yes very old thread, but I've probably spent 10 hours trying to figure this out, and this is the first site that has explained it in a way that I actually understand. Just resolved the conflict and my site is working like a charm. Thanks!

    ReplyDelete
  11. Old post I know. Just wanted say thanks. All I had to do was open up my jquery file and auto-replace every $( with jquery( and it worked fine with prototype.js on the same page.

    ReplyDelete
  12. is this page still alive..cuz i need help:)

    ReplyDelete
  13. Thank you, thank you, thank you!
    I got this small js for a meny and it came in conflict with the slideshow. Now it is working perfect! I did as Herbert and replace every $ with jQuery. I'm very new to this and it would helped a lot if you posted a example of "before" and "after" the replacing. Maby it is just me, but sometimes when you find solusion on the internet, it is hard to know where "to put" this perfect tip...
    Thanks again!

    ReplyDelete
  14. Thanks for the code, I will add it so I don't worried about jquery conflict.

    ReplyDelete