jQuery.noConflict( [removeAll ] )Returns: Object
Description: Relinquish jQuery's control of the $
variable.
-
version added: 1.0jQuery.noConflict( [removeAll ] )
-
removeAllType: BooleanA Boolean indicating whether to remove all jQuery variables from the global scope (including jQuery itself).
-
Many JavaScript libraries use $
as a function or variable name, just as jQuery does. In jQuery's case, $
is just an alias for jQuery
, so all functionality is available without using $
. If you need to use another JavaScript library alongside jQuery, return control of $
back to the other library with a call to $.noConflict()
. Old references of $
are saved during jQuery initialization; noConflict()
simply restores them.
If for some reason two versions of jQuery are loaded (which is not recommended), calling $.noConflict( true )
from the second version will return the globally scoped jQuery variables to those of the first version.
1
2
3
4
5
6
|
|
This technique is especially effective in conjunction with the .ready()
method's ability to alias the jQuery object, as within callback passed to .ready()
you can use $
if you wish without fear of conflicts later:
1
2
3
4
5
6
7
8
9
|
|
If necessary, you can free up the jQuery
name as well by passing true
as an argument to the method. This is rarely necessary, and if you must do this (for example, if you need to use multiple versions of the jQuery library on the same page), you need to consider that most plug-ins rely on the presence of the jQuery
variable and may not operate correctly in this situation.
Examples:
Map the original object that was referenced by $ back to $.
1
2
3
4
5
|
|
Revert the $ alias and then create and execute a function to provide the $ as a jQuery alias inside the function's scope. Inside the function the original $ object is not available. This works well for most plugins that don't rely on any other library.
1
2
3
4
5
6
7
8
|
|
Create a different alias instead of jQuery to use in the rest of the script.
1
2
3
4
5
6
7
|
|
Completely move jQuery to a new namespace in another object.
1
2
|
|
Result:
1
2
3
4
5
6
7
8
|
|
Load two versions of jQuery (not recommended). Then, restore jQuery's globally scoped variables to the first loaded jQuery.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
|