Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the DOMSubtreeModified event deprecated in DOM level 3?

Why is the DOMSubtreeModified event deprecated and what are we supposed to use instead?

like image 433
huyz Avatar asked Jul 12 '11 04:07

huyz


People also ask

Is mutation observer deprecated?

Note: Mutation Events (W3C DOM Level 3 Events) have been deprecated in favor of Mutation Observers (W3C DOM4).

What is DOMSubtreeModified?

DOMSubtreeModified. W3C Draft. This is a general event for notification of all changes to the document. It can be used instead of the more specific mutation and mutation name events.

Can I use DOMNodeInserted?

DOMNodeInserted is known to make dynamic pages slow, MDN even recommends not using it altogether, but doesn't provide any alternatives.


2 Answers

If you scroll down a bit, you see:

Warning! The MutationEvent interface was introduced in DOM Level 2 Events, but has not yet been completely and interoperably implemented across user agents. In addition, there have been critiques that the interface, as designed, introduces a performance and implementation challenge. A new specification is under development with the aim of addressing the use cases that mutation events solves, but in more performant manner. Thus, this specification describes mutation events for reference and completeness of legacy behavior, but deprecates the use of both the MutationEvent interface and the MutationNameEvent interface.

The replacement API is mutation observers, which are fully specified in the DOM Living Standard that supercedes all of the DOM level X silliness.

like image 199
Domenic Avatar answered Sep 23 '22 04:09

Domenic


I think the replacement will be mutation observers: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver

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       }       else if (mutationRecord.removedNodes.length > 0) {         //DOM node removed, do something       }     }     else if (mutationRecord.type === 'attributes') {       if (mutationRecord.attributeName === 'class') {         //class changed, do something       }     }   }); }); mutationObserver.observe(document.body, whatToObserve); 
like image 31
ralfthewise Avatar answered Sep 22 '22 04:09

ralfthewise