As you are
already aware that jQuery uses the $ sign as a shortcut for jQuery.
But what
if any other JavaScript frameworks like Prototype, MooTools, Knockout,
JavaScript MVC, Google Web Toolkit, Google Closure, or Ext JS also use the $
sign as a shortcut, or what happens if you are using more than one jQuery library
on your page.
"In such
cases, you have two different frameworks or libraries using the same shortcut,
which might result in your scripts stopping working."
So here
comes the benefit of the jQuery noConflict() method which releases the hold on the
$ shortcut identifier, so that other scripts can use it.
·
This
function gives control of the $ variable back to whichever library first implemented
it.
·
This
helps to make sure that jQuery doesn't conflict with the $ object of
other libraries.
Example
1: Use $ as a shortcut for the first library. You'll still be able to use "jQuery" for
another library to avoid conflict.
<html>
<head>
<script src="prototype.js"></script>
<script src="jquery.js"></script>
<script>
jQuery.noConflict();
//
Use jQuery via jQuery(...)
jQuery(document).ready(function () {
jQuery("div").hide();
});
//
Use Prototype with $(...)
$('txt').hide();
</script>
</head>
</html>
|
Example
2: Use $ as
shortcut for first library. If you want another library with shortcut or any other
nice name you want instead of writing jQuery, you can do it with noConflict
method as:
<html>
<head>
<script src="prototype.js"></script>
<script src="jquery.js"></script>
<script>
var
$j = jQuery.noConflict();
//
Use jQuery via $j(...)
$j(document).ready(function () {
$j("div").hide();
});
//
Use Prototype with $(...)
$('txt').hide();
</script>
</head>
</html>
|
Comments
Post a Comment