Tuesday, September 20, 2011

Configure jQuery to be Compatible with Other Libraries

We all find ourselves in situations where multiple libraries are needed, and because jQuery isn’t the only library that uses the $ alias, compatiblity issues sometimes pop up. Thankfully, this is easy to fix using jQuery’s noConflict(). You can even define a custom alias to replace the $:

var $j = jQuery.noConflict(); 

//Now you can use '$j' just like '$' 
$j("div").hide();
An alternate technique is to wrap jQuery calls in an anonymous function and pass jQuery in as a parameter. Then you can use whatever alias you want, including the ‘$’. This is especially useful for plugin authoring

(function($){ 
        $(document).ready(function(){ 
             //You can use normal jQuery syntax here 
        }); 
})(jQuery);

No comments:

Post a Comment