http://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/
The jQuery library and virtually all of its plugins are contained within the
jQuery
namespace. As a general rule, global objects are stored inside the jQuery namespace as well, so you shouldn't get a clash between jQuery and any other library (like prototype.js, MooTools, or YUI).
That said, there is one caveat:
by default, jQuery uses $
as a shortcut for jQuery
. Thus, if you are using another JavaScript library that uses the
$
variable, you can run into conflicts with jQuery. In order to avoid these conflicts, you need to put jQuery in no-conflict mode immediately after it is loaded onto the page and before you attempt to use jQuery in your page.
Putting jQuery Into No-Conflict Mode
When you put jQuery into no-conflict mode, you have the option of assigning a new variable name to replace the
$
alias.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<script src="prototype.js"></script>
<script src="jquery.js"></script>
var $j = jQuery.noConflict();
$j(document).ready(function() {
window.onload = function() {
var mainDiv = $( "main" );
|
In the code above, the
$
will revert back to its meaning in original library. You'll still be able to use the full function name
jQuery
as well as the new alias
$j
in the rest of your application. The new alias can be named anything you'd like:
jq
,
$J
,
awesomeQuery
, etc.
Finally, if you don't want to define another alternative to the full
jQuery
function name (you really like to use
$
and don't care about using the other library's
$
method), then there's still another approach you might try: simply add the
$
as an argument passed to your
jQuery( document ).ready()
function. This is most frequently used in the case where you still want the benefits of really concise jQuery code, but don't want to cause conflicts with other libraries.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<script src="prototype.js"></script>
<script src="jquery.js"></script>
jQuery( document ).ready(function( $ ) {
window.onload = function(){
var mainDiv = $( "main" );
|
This is probably the ideal solution for most of your code, considering that there'll be less code that you'll have to change in order to achieve complete compatibility.
Including jQuery Before Other Libraries
The code snippets above rely on jQuery being loaded after prototype.js is loaded. If you include jQuery before other libraries, you may use
jQuery
when you do some work with jQuery, but the
$
will have the meaning defined in the other library. There is no need to relinquish the
$
alias by calling
jQuery.noConflict()
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<script src="jquery.js"></script>
<script src="prototype.js"></script>
jQuery( document ).ready(function() {
window.onload = function() {
var mainDiv = $( "main" );
|
Summary of Ways to Reference the jQuery Function
Here's a recap of ways you can reference the jQuery function when the presence of another library creates a conflict over the use of the
$
variable:
Create a New Alias
The
jQuery.noConflict()
method returns a reference to the jQuery function, so you can capture it in whatever variable you'd like:
1
2
3
4
5
6
7
8
|
<script src="prototype.js"></script>
<script src="jquery.js"></script>
var $jq = jQuery.noConflict();
|
Use an Immediately Invoked Function Expression
You can continue to use the standard
$
by wrapping your code in an immediately invoked function expression; this is also a standard pattern for jQuery plugin authoring, where the author cannot know whether another library will have taken over the
$
. See the
Plugins section for more information about writing plugins.
1
2
3
4
5
6
7
8
9
10
11
12
|
<script src="prototype.js"></script>
<script src="jquery.js"></script>
|
Note that if you use this technique, you will not be able to use prototype.js methods inside the immediately invoked function that expect
$
to be prototype.js's
$
.
Use the Argument That's Passed to the jQuery( document ).ready()
Function
1
2
3
4
5
6
7
8
9
|
<script src="jquery.js"></script>
<script src="prototype.js"></script>
jQuery(document).ready(function( $ ) {
|
Or using the more concise syntax for the DOM ready function:
1
2
3
4
5
6
7
8
9
|
<script src="jquery.js"></script>
<script src="prototype.js"></script>
|