Skip to content

Instantly share code, notes, and snippets.

@namklabs
Created February 21, 2018 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save namklabs/8103a4d02d2858636771136bab0cc55a to your computer and use it in GitHub Desktop.
Save namklabs/8103a4d02d2858636771136bab0cc55a to your computer and use it in GitHub Desktop.
observeDOM.js
// https://stackoverflow.com/a/14570614/448640
var observeDOM = (function(){
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver,
eventListenerSupported = window.addEventListener;
return function(obj, callback){
if( MutationObserver ){
// define a new observer
var obs = new MutationObserver(function(mutations, observer){
if( mutations[0].addedNodes.length || mutations[0].removedNodes.length )
callback();
});
// have the observer observe foo for changes in children
obs.observe( obj, { childList:true, subtree:true });
}
else if( eventListenerSupported ){
obj.addEventListener('DOMNodeInserted', callback, false);
obj.addEventListener('DOMNodeRemoved', callback, false);
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment