Day 004 - No Conflict


As mentioned previously if you open up the development version of jQuery and search for : plus what you're looking for, in this case noConflict:, you'll see:

noConflict: function( deep ) {
    if ( window.$ === jQuery ) {
        window.$ = _$;
    }

    if ( deep && window.jQuery === jQuery ) {
        window.jQuery = _jQuery;
    }

    return jQuery;
}

It's also worth noting the jQuery API site has just had a sweet update and searching for noConflict, you'll find: http://api.jquery.com/jQuery.noConflict/

There's a few times, especially in WordPress builds where I've seen people fix multiple JavaScript libraries by constantly defining jQuery:

jQuery.noConflict();
jQuery(function() {
    // more code referencing jQuery
});

This can be improved using the following code to set $ back as an alias jQuery.

jQuery.noConflict();
// setting $ to alias to jQuery again
jQuery(document).ready(function($){
    $(function() {
        // now you can continue to use $ as an alias to jQuery
    });
});

Another alternative that's worth mentioning is using the following code which is essentially doing the same job. It is less descriptive, so not that great if you're a beginner trying to figure what is going on, but when you do, it's less code and widely used.

jQuery.noConflict();
(function($) {
    $(function() {
        // now you can continue to use $ as an alias to jQuery
    });
})(jQuery);

More on $(document).ready() in Day 005.