create your own

How to use jQuery's .noConflict() functionality

71
rate or flag this page

By Uzbekjon


About jQuery.noConflict();

jQuery is a great javascript library that makes your javascript development more interestin, exciting and effective. Most of us use jQuery without even knowing how it works or how to write jQuery code. We just take jQuery plugin and plug it in into our web pages. We go so crazy that we don't just plug in jQuery plugins, but also end up with MooTools, prototype.js plugins in our pages and that's probably when the problems start. All those plugins work perfect on their own but when you add another javascript framework plugin they start to conflict.

The good news is that jQuery made sure it does not conflict with other plugins. All you need to use is jQuery's .noConflict() method like this jQuery.noConflict(); before you write any jQuery code.

Example use

Here is an exaple of its use taken from jQuery.noConflict() post on jQuery blog:

<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>
 

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

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;

Print   —   Rate it:  up  down  flag this hub

Comments

RSS for comments on this Hub

hathibelagal profile image

hathibelagal  says:
4 months ago

Short.. but useful.

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional


  • No HTML is allowed in comments, but URLs will be hyperlinked
  • Comments are not for promoting your hubs or other sites

working