If we ever needed to listen for DOM events, one of the first options that comes to our mind might be to use setTimeout and periodically check the contents of the tag that we are interested in. Like in this example:
function myTest() { setTimeout(function() { console.log("Beep ..."); if(document.getElementById('root').innerHTML == 'Loading ...') { myTest(); } else { // do something alert("READY!"); } }, 1000); }
But this is intense JavaScript process, which could be done using more elegant way with the help of Mutation Observer.
Using Mutation Observer we could subscribe for different types of DOM events and trigger function on change.
example code:
<html> <body> <div id="root">loading ...</div> <script> setTimeout(function() { document.getElementById('root').innerHTML = "My content is loaded!"; }, 3000); </script> <script> var targetNode = document.getElementById('root'); var config = { attributes: true, childList: true, subtree: true, characterData:true }; var observer = new MutationObserver(function() { alert("#root div changed!"); // stop observing observer.disconnect(); }); observer.observe(targetNode, config); </script> </body> </html>
what is happening here:
– created a div tag with id=’root’ and added ‘Loading …’ text.
– created MutationObserver (line 14) to listen for DOM changes in ‘root’ folder.
– specified which DOM events will trigger the function in config
param (line 13)
– after 3 sec. setTimeout event fires and changes the content of the ‘root’ div, triggering DOM change event which will cause the function set in MutationObserver (line 14) to be executed.
– after showing the alert we could optionally disconnect and unsubscribe for DOM change events for this tag.
Here are some of the most used config properties
Mutation observer init properties:
childList
, attributes
, and/or characterData
must be true
when you call observe()
. Otherwise, a TypeError
exception will be thrown.
attributeFilter
Optional- An array of specific attribute names to be monitored. If this property isn’t included, changes to all attributes cause mutation notifications. No default value.
attributeOldValue
Optional- Set to
true
to record the previous value of any attribute that changes when monitoring the node or nodes for attribute changes; see Monitoring attribute values in MutationObserver for details on watching for attribute changes and value recording. No default value. attributes
Optional- Set to
true
to watch for changes to the value of attributes on the node or nodes being monitored. The default value isfalse
. characterData
Optional- Set to
true
to monitor the specified target node or subtree for changes to the character data contained within the node or nodes. No default value. characterDataOldValue
Optional- Set to
true
to record the previous value of a node’s text whenever the text changes on nodes being monitored. For details and an example, see Monitoring text content changes in MutationObserver. No default value. childList
Optional- Set to
true
to monitor the target node (and, ifsubtree
istrue
, its descendants) for the addition or removal of new child nodes. The default isfalse
. subtree
Optional- Set to
true
to extend monitoring to the entire subtree of nodes rooted attarget
. All of the otherMutationObserverInit
properties are then extended to all of the nodes in the subtree instead of applying solely to thetarget
node. The default value isfalse
.