jQuery का noConflict Mode
In this page:
var $j = jQuery.noConflict();
$j(selector).action();
noConflict क्या करता है
jQuery.noConflict() call करना global dollar symbol को उस library को वापस दे देता है जिसने इसे पहले define किया था, दोनों को एक-दूसरे को overwrite करने से रोकते हुए।
उदाहरण: What noConflict Does
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
</style>
</head>
<body>
<button id="runBtn">Run</button>
<button id="resetBtn">Reset</button>
<div id="output"></div>
<script>
jQuery("#runBtn").click(function() {
jQuery.noConflict();
jQuery("#output").text(typeof $);
});
jQuery("#resetBtn").click(function() {
window.$ = jQuery;
jQuery("#output").empty();
});
</script>
</body>
</html>
Dollar के बजाय jQuery इस्तेमाल करना
noConflict() call करने के बाद, $ shortcut release हो जाता है, इसलिए आपके code का हर selector इसके बजाय पूरा jQuery(...) नाम इस्तेमाल करने के लिए फिर से लिखा जाना चाहिए -- library collisions से बचने के बदले में typing की एक छोटी कीमत।
उदाहरण: Use jQuery Instead of Dollar
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
#demo :where(p, div, span, li, h1, h2, h3, h4, td, th, blockquote, label, section, article, aside) { border:1px solid #4a7fb5; background:#eaf2fb; padding:6px 10px; margin:4px 0; border-radius:4px; }
#demo :where(ul, ol) { border:1px dashed #888; background:#fafafa; padding:8px 8px 8px 32px; margin:6px 0; }
#demo :where(table) { border-collapse:collapse; }
#demo :where(td, th) { border-radius:0; }
#demo :where(input, select, textarea) { margin:4px 0; }
</style>
</head>
<body>
<button id="runBtn">Run</button>
<button id="resetBtn">Reset</button>
<section id="demo">
<p id="msg">Hi</p>
</section>
<div id="output"></div>
<script>
var original = $("#demo").html();
jQuery("#runBtn").click(function() {
jQuery.noConflict();
jQuery("#msg").text("Updated with jQuery()");
});
jQuery("#resetBtn").click(function() {
jQuery("#demo").html(original);
window.$ = jQuery;
jQuery("#output").empty();
});
</script>
</body>
</html>
एक Custom Alias बनाना
jQuery.noConflict() की return value को एक variable में assign करना, जैसे var $j = jQuery.noConflict(), आपको global $ symbol को बिल्कुल भी छुए बिना jQuery के लिए एक छोटा alias रखने देता है।
उदाहरण: Create a Custom Alias
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
#demo :where(p, div, span, li, h1, h2, h3, h4, td, th, blockquote, label, section, article, aside) { border:1px solid #4a7fb5; background:#eaf2fb; padding:6px 10px; margin:4px 0; border-radius:4px; }
#demo :where(ul, ol) { border:1px dashed #888; background:#fafafa; padding:8px 8px 8px 32px; margin:6px 0; }
#demo :where(table) { border-collapse:collapse; }
#demo :where(td, th) { border-radius:0; }
#demo :where(input, select, textarea) { margin:4px 0; }
</style>
</head>
<body>
<button id="runBtn">Run</button>
<button id="resetBtn">Reset</button>
<section id="demo">
<p id="msg">Hi</p>
</section>
<div id="output"></div>
<script>
var original = $("#demo").html();
jQuery("#runBtn").click(function() {
var $j = jQuery.noConflict();
$j("#msg").text("Updated with $j alias");
});
jQuery("#resetBtn").click(function() {
jQuery("#demo").html(original);
window.$ = jQuery;
jQuery("#output").empty();
});
</script>
</body>
</html>
Ready में Alias इस्तेमाल करना
वह custom alias हर जगह काम करता है जहाँ आप आमतौर पर $ इस्तेमाल करते, document ready block के अंदर भी -- $j(document).ready(...) एक बार $ कहीं और reassign होने के बाद $(document).ready(...) जैसा ही behave करता है।
उदाहरण: Use the Alias in Ready
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
#demo :where(p, div, span, li, h1, h2, h3, h4, td, th, blockquote, label, section, article, aside) { border:1px solid #4a7fb5; background:#eaf2fb; padding:6px 10px; margin:4px 0; border-radius:4px; }
#demo :where(ul, ol) { border:1px dashed #888; background:#fafafa; padding:8px 8px 8px 32px; margin:6px 0; }
#demo :where(table) { border-collapse:collapse; }
#demo :where(td, th) { border-radius:0; }
#demo :where(input, select, textarea) { margin:4px 0; }
</style>
</head>
<body>
<button id="runBtn">Run</button>
<button id="resetBtn">Reset</button>
<section id="demo">
<p id="msg">Hi</p>
</section>
<div id="output"></div>
<script>
var original = $("#demo").html();
jQuery("#runBtn").click(function() {
var $j = jQuery.noConflict();
$j(document).ready(function() {
$j("#msg").text("Ready fired using alias");
});
});
jQuery("#resetBtn").click(function() {
jQuery("#demo").html(original);
window.$ = jQuery;
jQuery("#output").empty();
});
</script>
</body>
</html>
Library Conflicts से बचना
noConflict() उन pages पर सबसे ज़्यादा मायने रखता है जो Prototype.js, MooTools, या दूसरी पुरानी libraries भी load करते हैं जो $ symbol भी claim करती हैं -- इसके बिना, जो भी library आख़िर में load होती है वह चुपचाप जीत जाती है और दूसरी का code तोड़ देती है।
उदाहरण: Avoid Library Conflicts
<!DOCTYPE html>
<html>
<head>
<script>
window.$ = function() { return "other-library"; };
</script>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
body { font-family: sans-serif; padding: 12px; }
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
button.secondary, #resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
input[type=text] { padding: 6px 8px; margin: 0 6px 8px 0; border: 1px solid #888; border-radius: 4px; font: 14px sans-serif; }
.stage { border: 2px dashed #888; border-radius: 4px; padding: 10px; margin: 8px 0; min-height: 40px; background: #fafafa; }
.stage::before { content: attr(data-label); display: block; font-size: 12px; color: #666; margin-bottom: 6px; }
.item { border: 1px solid #4a7fb5; background: #eaf2fb; padding: 6px 10px; margin: 4px 0; border-radius: 4px; }
.fresh { background: #ffe066 !important; border-color: #d4a600 !important; }
.done { text-decoration: line-through; color: #a33; }
ul.stage { padding-left: 32px; }
ul.stage:empty::after { content: "(empty)"; color: #999; }
ul.stage > li { border: 1px solid #4a7fb5; background: #eaf2fb; padding: 6px 10px; margin: 4px 0; border-radius: 4px; }
.box { border: 2px solid #4a7fb5; background: #f3f8fd; border-radius: 4px; padding: 8px 10px; margin: 8px 0; min-height: 54px; }
.box .label { display: block; font-weight: bold; margin-bottom: 6px; color: #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; white-space:pre-wrap; }
#output:empty::before { content: "Output appears here"; color: #888; }
</style>
</head>
<body>
<button id="runBtn">Run noConflict()</button><button id="resetBtn" class="secondary">Reset</button>
<div id="output"></div>
<script>
jQuery("#runBtn").click(function () {
jQuery.noConflict();
jQuery("#output").text("$() now returns: " + $());
});
jQuery("#resetBtn").click(function () {
window.$ = jQuery;
jQuery("#output").empty();
});
</script>
</body>
</html>
jQuery.noConflict()call करना और फिर भी$("#msg")लिखना, जो fail हो जाता है क्योंकि$अब दूसरी library का है।- jQuery file load होने से पहले, या
$की भी ज़रूरत वाली किसी दूसरी library के बादjQuery.noConflict()call करना, इसलिए असल में कुछ भी release नहीं होता। noConflictcall करना लेकिन return value save न करना, जैसेvar $j = jQuery.noConflict();, और फिर इस्तेमाल करने के लिए कोई छोटा alias न होना।
- jQuery एक JavaScript library है जो page के साथ काम करना simple बनाती है, और इसे एक script tag या CDN से जोड़ा जाता है।
- jQuery syntax elements select करता है और उन पर action लेता है, आमतौर पर document ready event के अंदर।
- CDN fallbacks और
noConflictmode jQuery को reliably load होने और दूसरी libraries के साथ coexist करने में मदद करते हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: