Library for DOM mutation events?

I need to trigger an action when adding content to a web page. Updates can have a different nature (AJAX, delayed scripts, for example, user action) and are not under my control.

I wanted to use DOM mutation events, but they are not available in all browsers. Are there cross-browser libraries for this that offer a backup plan?

Also, I would be interested to know if Internet Explorer 9 supports these mutational DOM events.

Thanks!

+4
source share
4 answers

There are two ways to do this ...

Firstly, you can take a snapshot of dom ( var snap = document.body ), compare it with dom 100ms later, and then reset snap to body again. I will allow you to be creative in comparing them: iteration seems to be the general answer. This is not very.

Another option is to have a special function in any functions you create that add / remove elements in your application. This function will iterate over only the elements that you add (or destroy), and look for a match.

 /* Shim matchesSelector */ if (!Element.prototype.matchesSelector) { Element.prototype.matchesSelector = Element.prototype.matches || Element.prototype.webkitMatchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.msMatchesSelector || Element.prototype.oMatchesSelector; } function addingToDom(elm){ /* Whichever method you want to use to map selectors to functions */ var callbacks = array(['.class', fn-reference1], ['#id', fn-reference2], ['div', fn-reference3]); /* go through all the elements you're adding */ for (var i = 0; i<elm.length; ++i){ /* go through all the selectors you're matching against */ for (var k = 0; k<callbacks.length ++k){ if(elm[i].matchesSelector(callbacks[k][0])){ /* call function with element passed as parameter */ callbacks[k][1](elm[i]); } } } } 

It may not be perfect, but it should give you an idea of ​​where to start. Call this function (addToDom) by passing the elements you just added as an argument. Create a similar function to delete items (or the same function, but set up a separate callback array).

This is an extrapolation of the current (large and dirty) code that I use to test some ideas. I checked the match selector even like ie7 with this pad, and it seems to work great!

I considered, but did not consider, the possibility that the element has some parameter that can be set as a function called upon addition, passing itself as a parameter. BUT, perhaps far-fetched.

+1
source

You can easily call an action by setting a function and using JSONP for AJAX requests to call this function. The same function can be called during user action.

0
source

Well ... here's the idea of ​​saying whether an element has been added: add a class to each element on the page, then use the query to find every element that does not have this querySelectorAll(':not(.myclass)') class, then scroll through those.

You still have to run it at intervals, but querySelectorAll is fast, since it is a native browser, and if it returns 'empty' ( false ? null ? I don’t know, from my point of view), you are not doing anything, and that should be minimal overhead capital.

0
source

I just stumbled upon an interesting hack that relies on css events:

http://www.backalleycoder.com/2012/04/25/i-want-a-damnodeinserted/

According to the author, this works in IE10, Firefox 5+, Chrome 3+, Opera 12, Android 2.0+, Safari 4+ and almost every version if iPhone is Safari.

0
source

All Articles