Last active
August 8, 2017 17:40
-
-
Save SidIcarus/eb6e5c72c9b3681879615eb20bb58429 to your computer and use it in GitHub Desktop.
MutationObserver
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
/** | |
* Supports IE9+, FF, Webkit | |
* @param callback | |
*/ | |
function watchElementState(callback) { | |
var MutationObserver = | |
window.MutationObserver || window.WebKitMutationObserver, | |
eventListenerSupported = window.addEventListener; | |
// callback.bind(this); | |
// If you have multiple anonymous functions within your MutationObserver, | |
// binding your scope to a variable is easier then continuously binding | |
// it to the functions | |
var self = this; | |
var node; // DOM Node | |
if (MutationObserver) { | |
var observer = new MutationObserver(function(mutations) { | |
// your logic | |
callback(); | |
}); | |
// https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver | |
observer.observe(node, { | |
attributes: true, | |
attributeOldValue: true, | |
}); | |
} else if (eventListenerSupported) { | |
var changeAdd = "<replaceMe>Added", | |
changeRemove = "<replaceMe>Removed"; | |
element.addEventListener(changeAdd, callback, false); | |
element.addEventListener(changeRemove, callback, false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment