← Back to JavaScript Course | Chapter 10: DOM Advanced | Lesson 3 of 8

JS का MutationObserver

MutationObserver page के किसी हिस्से को देखता है और आपको बताता है कि अंदर कुछ बदलने पर, जैसे एक security camera जो movement पर आपको alert करता है। यह नए elements, attributes, या text नोटिस कर सकता है।
Syntax
javascript
const observer = new MutationObserver(callback);
observer.observe(element, { childList: true, attributes: true });
observer.disconnect();

MutationObserver क्या है?

MutationObserver बदलावों के लिए किसी DOM element को देखता है। यह जोड़े गए, हटाए गए, और बदले गए nodes का पता लगा सकता है।

यह दूसरे code (जैसे कोई third-party widget) द्वारा किए गए DOM बदलावों पर react करने के लिए उपयोगी है जिनमें आप अपने खुद के event handlers से सीधे hook नहीं कर सकते।

उदाहरण: What Is MutationObserver?

javascript
<div id="box">Original</div>
<script>
  // Declare the constant `observer` as a new `MutationObserver` instance
  const observer = new MutationObserver(() => console.log("Something changed"));
  observer.observe(document.getElementById("box"), { childList: true, attributes: true, characterData: true, subtree: true });
  document.getElementById("box").textContent = "Changed";
</script>

जोड़े गए Nodes देखना

childList option आपको यह पता लगाने देता है कि कब child nodes जोड़े या हटाए जाते हैं। यह आपको खासतौर पर structural बदलावों पर react करने देता है — जैसे किसी list में एक नया item आना — बिना असंबंधित style या attribute updates के बारे में भी notify हुए।

उदाहरण: Watching Added Nodes

javascript
<ul id="list"><li>Item 1</li></ul>
<script>
  // Declare the constant `observer` as a new `MutationObserver` instance
  const observer = new MutationObserver((mutations) => console.log("Nodes changed:", mutations.length));
  // Call `observer.observe(document.getElementById("list"), { childList: true })`
  observer.observe(document.getElementById("list"), { childList: true });
  // Declare the constant `li`, set to `document.createElement("li")`
  const li = document.createElement("li");
  // Assign "Item 2" to `li.textContent`
  li.textContent = "Item 2";
  // Call `document.getElementById("list").appendChild(li)`
  document.getElementById("list").appendChild(li);
</script>

Attribute Changes देखना

attributes option class या data attributes जैसे element attributes में बदलाव पता लगाता है। इसका इस्तेमाल आमतौर पर यह पता लगाने के लिए किया जाता है कि कब दूसरे code द्वारा toggle की गई कोई class (जैसे कोई loading या active state) असल में बदलती है।

उदाहरण: Watching Attribute Changes

javascript
<div id="box" class="idle">Status</div>
<script>
  // Declare the constant `observer` as a new `MutationObserver` instance
  const observer = new MutationObserver(() => console.log("Class changed:", document.getElementById("box").className));
  // Call `observer.observe(document.getElementById("box"), { attributes: true })`
  observer.observe(document.getElementById("box"), { attributes: true });
  document.getElementById("box").className = "active";
</script>

Text Changes देखना

characterData option text nodes में बदलाव देखता है। subtree descendants को शामिल कर सकता है। यही combination आपको visible text content में edits पता लगाने देता है, जो वरना कोई childList या attributes mutation trigger नहीं करते।

उदाहरण: Watching Text Changes

javascript
<p id="text">Hello</p>
<script>
  // Declare the constant `observer` as a new `MutationObserver` instance
  const observer = new MutationObserver(() => console.log("Text changed"));
  // Call `observer.observe(document.getElementById("text"), { characterData: true, subtree: true })`
  observer.observe(document.getElementById("text"), { characterData: true, subtree: true });
  document.getElementById("text").firstChild.textContent = "Updated";
</script>

एक Observer रोकना

जब आपको किसी observer की ज़रूरत न रहे तो disconnect कॉल करें। यह अनावश्यक काम से बचाता है। चूंकि observers explicitly रोके जाने तक चलते (और resources consume करते) रहते हैं, जिस observer की आपको ज़रूरत नहीं उसे disconnect करना सिर्फ एक optimization नहीं, एक अच्छी practice है।

उदाहरण: Stopping an Observer

javascript
<div id="box">Content</div>
<script>
  const observer = new MutationObserver(() => console.log("Changed"));
  observer.observe(document.getElementById("box"), { childList: true });
  observer.disconnect(); // stop watching, avoids unnecessary work
  document.getElementById("box").textContent = "Never logged";
</script>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. observer callback के अंदर DOM को ऐसे बदलना कि यह खुद को trigger कर दे, जो एक infinite loop का कारण बनता है।
  2. बिना options object के observe() कॉल करना, जो TypeError throw करता है; childList, attributes या characterData set करें।
  3. disconnect() भूल जाना, इसलिए observer चलता रहता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.