Jquery Noconflict()

jQuery - The noConflict() Method


What if you lot want to job other frameworks on your pages, piece all the same using jQuery?

jQuery as well as Other JavaScript Frameworks

As you lot already know; jQuery uses the $ sign equally a shortcut for jQuery.
There are many other pop JavaScript frameworks like: Angular, Backbone, Ember, Knockout, as well as more.
What if other JavaScript frameworks equally good job the $ sign equally a shortcut?
If ii dissimilar frameworks are using the same shortcut, ane of them powerfulness cease working.
The jQuery squad convey already idea close this, as well as implemented the noConflict() method.

The jQuery noConflict() Method

The noConflict() method releases the concur on the $ shortcut identifier, as well as thus that other scripts tin job it.
You tin of course of written report all the same job jQuery, exactly yesteryear writing the sum advert instead of the shortcut:

Example

$.noConflict();
jQuery(document).ready(function(){
    jQuery("button").click(function(){
        jQuery("p").text("jQuery is all the same working!");
    });
});
You tin equally good practise your ain shortcut real easily. The noConflict() method returns a reference to jQuery, that you lot tin salve inward a variable, for afterwards use. Here is an example:

Example

var jq = $.noConflict();
jq(document).ready(function(){
    jq("button").click(function(){
        jq("p").text("jQuery is all the same working!");
    });
});
If you lot convey a block of jQuery code which uses the $ shortcut as well as you lot practise non desire to modify it all, you lot tin croak the $ sign inward equally a parameter to the induce method. This allows you lot to access jQuery using $, within this business office - exterior of it, you lot volition convey to job "jQuery":

Example

$.noConflict();
jQuery(document).ready(function($){
    $("button").click(function(){
        $("p").text("jQuery is all the same working!");
    });
});


Comments

Popular posts from this blog

What Are The Main Components of a Computer System

Top Qualities To Look For In An IT Support Team

How To Integrate Google Adwords Api Into Codeigniter?