Is it possible to observer mutations on a DOM node that doesn't exist yet?
Example:
My app creates a div at some point: <div id="message" data-message-content="foo" data-message-type="bar" />
.
I want to watch for the creation & change of this div.
var mutationObserver = new MutationObserver(function(mutations){
// Some code to handle the mutation.
});
mutationObserver.observe(
document.querySelector('#message'),
{
attributes: true,
subtree: true,
childList: true,
characterData: false
}
);
);
Right now this returns an error since #message
is null (the div hasn't been created yet).
Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'.
An obvious solution is to watch the body
and check if any of the mutations are the creation of div#Message
, but this seems like a bad idea / or possibly bad for performance.
Only an existing node can be observed.
But don't worry, since getElementById is insanely fast compared to enumeration of all mutations' added nodes, waiting for the element to appear won't be taxing at all as you will see in Devtools -> Profiler panel.
function waitForAddedNode(params) {
new MutationObserver(function(mutations) {
var el = document.getElementById(params.id);
if (el) {
this.disconnect();
params.done(el);
}
}).observe(params.parent || document, {
subtree: !!params.recursive || !params.parent,
childList: true,
});
}
Usage:
waitForAddedNode({
id: 'message',
parent: document.querySelector('.container'),
recursive: false,
done: function(el) {
console.log(el);
}
});
Always use the devtools profiler and try to make your observer callback consume less than 1% of CPU time.
subtree: false
)for (var i=0 ....)
loop, and MutationObserver callback may fire 100 times per second with dozens, hundreds or thousands of addedNodes
in each batch of mutations on complex modern pages.for (v of something)
inside MutationObserver callback unless you transcompile and the resultant code runs as fast as the classic for
loop.