MutationObserver
provides developers with a way to react to changes in a DOM. It is designed as a replacement for Mutation Events defined in the DOM3 Events specification.
MutationObserver()
Constructor for instantiating new DOM mutation observers.
new MutationObserver( function callback );
callback
MutationRecord
. The second is this MutationObserver
instance.void observe( |
void disconnect(); |
Array takeRecords(); |
observe()
Registers the MutationObserver
instance to receive notifications of DOM mutations on the specified node.
void observe( Node target, MutationObserverInit options );
target
Node
on which to observe DOM mutations.options
MutationObserverInit
object, specifies which DOM mutations should be reported.disconnect()
Stops the MutationObserver
instance from receiving notifications of DOM mutations. Until the observe()
method is used again, the observer's callback will not be invoked.
void disconnect();
Note: According to specification a MutationObserver is deleted by the garbage collector if the target element is deleted.
takeRecords()
Empties the MutationObserver
instance's record queue and returns what was in there.
Array takeRecords();
Returns an Array of MutationRecord
s.
MutationObserverInit
MutationObserverInit
is an object which can specify the following properties:
childList
, attributes
, or characterData
must be set to true
. Otherwise, "An invalid or illegal string was specified" error is thrown.Property | Description |
---|---|
childList | Set to true if additions and removals of the target node's child elements (including text nodes) are to be observed. |
attributes | Set to true if mutations to target's attributes are to be observed. |
characterData | Set to true if mutations to target's data are to be observed. |
subtree | Set to true if mutations to target and target's descendants are to be observed. |
attributeOldValue | Set to true if attributes is set to true and target's attribute value before the mutation needs to be recorded. |
characterDataOldValue | Set to true if characterData is set to true and target's data before the mutation needs to be recorded. |
attributeFilter | Set to an array of attribute local names (without namespace) if not all attribute mutations need to be observed. |
The following example was adapted from this blog post.
// Select the node that will be observed for mutations var targetNode = document.getElementById('some-id'); // Options for the observer (which mutations to observe) var config = { attributes: true, childList: true }; // Callback function to execute when mutations are observed var callback = function(mutationsList) { for(var mutation of mutationsList) { if (mutation.type == 'childList') { console.log('A child node has been added or removed.'); } else if (mutation.type == 'attributes') { console.log('The ' + mutation.attributeName + ' attribute was modified.'); } } }; // Create an observer instance linked to the callback function var observer = new MutationObserver(callback); // Start observing the target node for configured mutations observer.observe(targetNode, config); // Later, you can stop observing observer.disconnect();
Specification | Status | Comment |
---|---|---|
DOM The definition of 'MutationObserver' in that specification. | Living Standard |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 18 -webkit 26 | (Yes) | 14 (14) | 11 | 15 | 6.0 -webkit 7 |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | 4.4 | 18 -webkit 26 | (Yes) | 14.0 (14) | 11 (8.1) | 15 | 6 -webkit 7 |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver