jQuery - noConflict() Method
How can you use jQuery together with other frameworks on the same page?
jQuery and Other JavaScript Frameworks
As you have already learned, jQuery uses the $ sign as a shorthand for jQuery.
What if another JavaScript framework also uses the $ sign as a shorthand?
Some other JavaScript frameworks include: MooTools, Backbone, Sammy, Cappuccino, Knockout, JavaScript MVC, Google Web Toolkit, Google Closure, Ember, Batman, and Ext JS.
Some of these frameworks also use the $ sign as shorthand (just like jQuery). If two different frameworks you are using share the same shorthand symbol, it may cause your scripts to stop working.
The jQuery team took this into account and implemented the noConflict() method.
jQuery noConflict() Method
The noConflict() method releases control of the $ identifier so that other scripts can use it.
Of course, you can still use jQuery by its full name instead of the shorthand:
Example
$.
noConflict
(
)
;
jQuery
(
document
)
.
ready
(
function
(
)
{
jQuery
(
"
button
"
)
.
click
(
function
(
)
{
jQuery
(
"
p
"
)
.
text
(
"
jQuery is still working!
"
)
;
}
)
;
}
)
;
Try it »
You can also create your own shorthand. noConflict() returns a reference to jQuery, which you can store in a variable for later use. See this example:
Example
var
jq
= $.
noConflict
(
)
;
jq
(
document
)
.
ready
(
function
(
)
{
jq
(
"
button
"
)
.
click
(
function
(
)
{
jq
(
"
p
"
)
.
text
(
"
jQuery is still working!
"
)
;
}
)
;
}
)
;
Try it »
If your jQuery code blocks use the $ shorthand and you do not want to change this shortcut, you can pass the $ symbol as a variable to the ready method. This lets you use the $ symbol inside the function - while outside the function, you still have to use "jQuery":
Example
$.
noConflict
(
)
;
jQuery
(
document
)
.
ready
(
function
(
$
)
{
$
(
"
button
"
)
.
click
(
function
(
)
{
$
(
"
p
"
)
.
text
(
"
jQuery is still working!
"
)
;
}
)
;
}
)
;
Try it »