Skip to content

Instantly share code, notes, and snippets.

@SidIcarus
Last active August 8, 2017 17:40
Show Gist options
  • Save SidIcarus/eb6e5c72c9b3681879615eb20bb58429 to your computer and use it in GitHub Desktop.
Save SidIcarus/eb6e5c72c9b3681879615eb20bb58429 to your computer and use it in GitHub Desktop.
MutationObserver
/**
* 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