Created
March 12, 2014 14:33
-
-
Save rdhyee/9508158 to your computer and use it in GitHub Desktop.
code bit to observe changes in DOM. Based on http://stackoverflow.com/a/18970264
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var whatToObserve = {childList: true, attributes: true, subtree: true, attributeOldValue: true, attributeFilter: ['class', 'style']}; | |
var mutationObserver = new MutationObserver(function(mutationRecords) { | |
$.each(mutationRecords, function(index, mutationRecord) { | |
if (mutationRecord.type === 'childList') { | |
if (mutationRecord.addedNodes.length > 0) { | |
//DOM node added, do something | |
console.log("DOM node added"); | |
} | |
else if (mutationRecord.removedNodes.length > 0) { | |
//DOM node removed, do something | |
console.log("DOM node removed"); | |
} | |
} | |
else if (mutationRecord.type === 'attributes') { | |
if (mutationRecord.attributeName === 'class') { | |
//class changed, do something | |
console.log("class changed"); | |
} | |
} | |
}); | |
}); | |
mutationObserver.observe(document.body, whatToObserve); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment