Reputation: 6544
I made this fiddle that listens for a few different MutationEvents
, specifically DOMCharacterDataModified
, DOMSubtreeModified
, and DOMNodeInserted
when changing the innerHTML
of a div.
http://jsfiddle.net/newtang/kysTm/15/
Interestingly, Chrome shows:
DOMCharacterDataModified
, DOMSubtreeModified
while Firefox 5 shows: DOMNodeInserted
.
I'm not actually sure who's correct. I found this old Mozilla bug: https://bugzilla.mozilla.org/show_bug.cgi?id=368133 and the W3 docs (http://www.w3.org/TR/DOM-Level-2-Events/events.html), but I don't find either particularly enlightening.
Does anyone know what the proper behavior is? I'd love to file a bug on someone so it's consistent.
Upvotes: 3
Views: 4063
Reputation: 35064
WebKit has an "optimization" where if you set innerHTML to something that contains no markup and the first child of the node you're setting it on is a Text node they will modify the text in the textnode directly instead of doing what the spec calls for (removing all the children and creating a new Text child). Hence the difference in mutation events.
Upvotes: 3
Reputation: 117324
Firefox's behaviour seems to be correct. You setting the innerHTML, so you do not change the characterData of an existing textNode, you insert a new textNode and remove any existing contents .(You may also observe DOMNodeRemoved in your example, you'll see that it fires too)
See the difference when really modifying the data of a textNode: http://jsfiddle.net/doktormolle/yQu8v/
Upvotes: 2