Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MutationObserver not working

Consider the following code:

var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;

var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    console.log(mutation.target.nodeName);
  });
});

observer.observe(document, {
  attributes: true,
  childList: true,
  characterData: true
});
<div>
  <ol contenteditable oninput="">
    <li>Press enter</li>
  </ol>
</div>

which is a slight modification of this.

Interacting with the jsbin version page does not produce any log. Where am I wrong? Notice that if I substitute line

  observer.observe(document, {

with

  observer.observe(document.querySelector('ol'), {

the script turns on working...

like image 249
tic Avatar asked Jun 19 '15 20:06

tic


People also ask

What is MutationObserver?

The MutationObserver interface provides the ability to watch for changes being made to the DOM tree. It is designed as a replacement for the older Mutation Events feature, which was part of the DOM3 Events specification.

How do I make the mutationobserver work?

However, to make the MutationObserver works, at least one of childList, attributes, or characterData needs to be set to true, otherwise the observer () method will throw an error. The following example illustrates how to use the childList property of the mutation options object to monitor for the child node changes.

What is mutation observer in Node JS?

MutationObserver The MutationObserver interface provides the ability to watch for changes being made to the DOM tree. It is designed as a replacement for the older Mutation Events feature, which was part of the DOM3 Events specification.

Why is mutationobserver not defined in JavaScript?

You may be in some scope where MutationObserver is not defined. It is not some inherent feature of javascript, but a property of the window object. If the window object is not the global in your scope then you’ll have to explicitly specify which window you want a mutation observer for.

How to observe changes in DOM using mutationobserver?

First, define the callback function that will execute when the DOM changes: Second, create a MutationObserver object and pass the callback into the MutationObserver () constructor: Third, call the observe () method to start observing the DOM changes. The observe () method has two parameters.


1 Answers

It doesn't appear to work because you are not mutating anything that you are observing. You are neither changing

  • attributes (attributes: true) of the document node (which is understandable, since document doesn't have attributes)
  • child nodes (childList: true): the only child node of document is the <html> node, and you are not removing or replacing it.
  • character data (characterData: true): you are not changing any Text, Comment, or ProcessingInstruction children of document (also understandable because document cannot have such children).

If you replace the <html> node, you can see that the mutation observer works just as configured.

var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;    var observer = new MutationObserver(function(mutations) {    mutations.forEach(function(mutation) {      console.log(mutation.target.nodeName);    });  });    observer.observe(document, {    attributes: true,    childList: true,    characterData: true  });    document.replaceChild(document.createElement('div'), document.documentElement);

What you are doing is changing the content of the ol element, which is a descendant of document.

If you want to listen to these kind of changes, you have to set subtree to true:

observer.observe(document, {   attributes: true,   childList: true,   subtree: true,   characterData: true }); 

More information in the MDN documentation.

var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;    var observer = new MutationObserver(function(mutations) {    mutations.forEach(function(mutation) {      console.log(mutation.target.nodeName);    });  });    observer.observe(document, {    attributes: true,    childList: true,    subtree: true,    characterData: true  });
<div>    <ol contenteditable oninput="">      <li>Press enter</li>    </ol>  </div>
like image 191
Felix Kling Avatar answered Sep 28 '22 23:09

Felix Kling