Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOM mutation events replacement

Tags:

Since DOM mutation is marked as deprecated by the w3c (see http://www.w3.org/TR/DOM-Level-3-Events/#events-mutationevents), is there an (fast) alternative way to detect attribute modification in the DOM ?

like image 379
Franck Freiburger Avatar asked Mar 24 '11 09:03

Franck Freiburger


People also ask

What are DOM mutations?

The Mutation Events API was specified around the year 2000. It should provide an easy way to observe and react to changes in a DOM tree. It consisted of several different events, such as DOMNodeRemoved and DOMAttrModified . Events were fired directly, i.e. synchronously, after the node changed.

Is MutationObserver deprecated?

Use of Mutation Events is deprecated. Use MutationObserver instead. How do I find out the code responsible for triggering this message? It's likely due to a third-party library using the deprecated events.

What are mutation events?

The MutationEvent interface provides event properties that are specific to modifications to the Document Object Model (DOM) hierarchy and nodes.


1 Answers

The reason that mutation events was deprecated was because of huge performance issues.

The replacement is Mutation Observers, look at http://updates.html5rocks.com/2012/02/Detect-DOM-changes-with-Mutation-Observers and https://developer.mozilla.org/en/DOM/DOM_Mutation_Observers

Information about mutations is delivered to observers as an ordered sequence of MutationRecords, representing an observed sequence of changes that have occurred

Sample usage:

    var observer = new MutationObserver(function(mutationRecords) {     // Handle mutations      });      observer.observe(myNode,      {  // options:      subtree: true,  // observe the subtree rooted at myNode      childList: true,  // include information childNode insertion/removals      attribute: true  // include information about changes to attributes within the subtree     }); 

This is supported in Chrome 18 and Firefox and Webkit nightly builds. Firefox 14 will also be supporting this feature.

like image 187
ama2 Avatar answered Oct 22 '22 00:10

ama2