tags:

views:

166

answers:

2

I want to compare the change of a DOM node after a user event is fired on it; but I don't know on which element a user would fire, so my idea is to (1) save the DOM tree before an event and (2) compare the saved tree with the updated DOM tree when an event is fired.

My question are (1) is there any better way? and (2) if there is no other way, what would be the fast algorithm to compare two DOM trees?

+1  A: 

Depends on how complex a change you're expecting, but one possible "better" way would be to make use of the built in DOM events. Specifically, the DOMSubtreeModified event will fire whenever the part of the DOM tree you're watching gets changed.

Alconja
The problem is that I don't know which part of DOM would change...
Paul
The attach a `DOMSubtreeModified` listener to the document.
Tim Down
The built-in DOM events don't work in IE, so this may not be a viable solution anyway.
Tim Down
Should more than one answer be selected as accepted answer, I would have added this answer as an accepted answer too...
Paul
+1  A: 

Since events bubble up, you could simply attach an event listener to the window object which catches all events, regardless of where they originated. You can examine the passed event object for event.target to see where it originated.

deceze
guys, what would you recommend to check the change of a DOM tree, such as a node deleted or added.
Paul
@Paul You aren't in control of the functions that will be executed on the DOM elements? Otherwise you should know. ;)
deceze
@deceze, no, I'm not because the change of DOM might happen in runtime...
Paul