jQuery noConflict
jQuery leans heavily on a single, very short global variable: the dollar sign ($). It is convenient, but jQuery is not the only library that wants that name. Older libraries such as Prototype, MooTools, and many small utility scripts also claim $ for themselves. When two libraries reach for the same global, whichever one loads last wins, and the other one quietly breaks. The noConflict() method is jQuery's polite way of stepping aside so everyone can share the page.
Why conflicts happen
When you include jQuery on a page, it defines two global names: jQuery and its famous shortcut $. The long name jQuery is unlikely to clash with anything, but $ is a popular one-character variable that other scripts grab too. If another library defines $ after jQuery loads, calls like $('#menu') suddenly run the wrong code, and you get confusing errors that have nothing to do with your own logic.
- jQuery always registers the full name jQuery in addition to $.
- The $ shortcut is the part most likely to collide with another library.
- The last script to define $ overwrites any earlier definition.
- noConflict() lets jQuery release $ and hand control back to whoever had it first.
Releasing the $ shortcut
Calling $.noConflict() tells jQuery to give up the $ variable and restore whatever value $ held before jQuery loaded. After that call, $ no longer means jQuery, but the full jQuery name still works everywhere. You simply write jQuery instead of $ from that point on.
Give up $ and use the full jQuery name
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<button>Click me</button>
<p>This paragraph will change when the button is clicked.</p>
<script>
$.noConflict();
jQuery(document).ready(function () {
jQuery("button").click(function () {
jQuery("p").text("jQuery still works using its full name.");
});
});
</script>
</body>
</html>Typing jQuery over and over gets tiring, so noConflict() actually returns the jQuery object. That means you can capture it in your own custom alias and keep your code short without touching the global $ at all.
Create your own short alias
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<p id="status">Waiting...</p>
<script>
var jq = $.noConflict();
jq(document).ready(function () {
jq("#status").text("Now $ is free, and jq is our jQuery.");
});
</script>
</body>
</html>Using $ safely inside a scope
Often you release $ globally but still want the familiar $ inside your own code. You can pass jQuery into the ready() callback as a parameter named $. Inside that function $ means jQuery, while outside it the global $ still belongs to the other library. This is the cleanest pattern for most real projects.
Keep $ local to your function
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<button>Hide paragraph</button>
<p>This paragraph will be hidden when the button is clicked.</p>
<script>
$.noConflict();
jQuery(document).ready(function ($) {
$("button").click(function () {
$("p").hide();
});
});
</script>
</body>
</html>Exercise: jQuery noConflict
What does $.noConflict() actually do?