34

Possible Duplicate:
Detect changes in the DOM

I need to setup an event handler, which should fire whenever something is appended/changed/removed on the DOM. This answer does not work in my case. The DOM can be modified by third party developers, and on each DOM change, we need to run our script to validate the code. The event should not fire on input/textarea/select change.

We do not target all browsers. As long as it works on Webkit (Chrome, Safari), it's good enough.

Any ideas?

Community
  • 1
  • 1
Sherzod
  • 5,041
  • 10
  • 47
  • 66
  • 6
    Does anyone have the link to the question that this is an exact duplicate of? I came here from google. – travis Apr 16 '12 at 18:05
  • 5
    @travis: not exact duplicate, but close: http://stackoverflow.com/questions/3219758/detect-changes-in-the-dom – Sherzod Apr 16 '12 at 18:09
  • 2
    @travis: I actually found the answer that suits my case: using `DOMSubtreeModified` event, but you need to check browser support. Webkit works fine with it. – Sherzod Apr 16 '12 at 18:11
  • Yeah, I'm looking for something that works back to at least IE8. – travis Apr 16 '12 at 18:27

2 Answers2

9

DOM mutation events (I believe not supported in all browsers) .. see http://en.wikipedia.org/wiki/DOM_events#Common.2FW3C_events

Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
1

I don't think so there is any such event across all the browsers. I would go with a custom event and trigger it whenever I manipulate the dom.

//Subscribe to domChanged event
$(document).bind('domChanged', function(){
     alert('Dom changed');
});

//Trigger the domChanged event
$(document).trigger('domChanged');
HBP
  • 15,685
  • 6
  • 28
  • 34
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
  • 19
    The problem states dom is maniuplated by a 3rd party. so this won't work. – Myster Oct 09 '12 at 08:53
  • 1
    `domChanged` should be a constant defined in an events module. Eg, `export const domInjected = 'myDOMInjected';` (ES6) You may also want to be careful of using something may already exist in the near future... such as DOMNodeInserted – Ray Foss Aug 17 '16 at 12:26