Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is "DOMNodeInserted" event called?

DOMNodeInserted event is called when the node "be appended to", or "be appended"?

I ask this because the following code:

function AddTextToContainer () {
    var textNode = document.createTextNode ("My text");
    var container = document.getElementById ("container");

    if (container.addEventListener) {
        container.addEventListener ('DOMNodeInserted', OnNodeInserted, false);
    }
    container.appendChild (textNode);
}

and that:

function AddTextToContainer () {
   var textNode = document.createTextNode ("My text");
   var container = document.getElementById ("container");

   if (textNode.addEventListener) {
       textNode.addEventListener ('DOMNodeInserted', OnNodeInserted, false);
   }
   container.appendChild (textNode);
}

Both invoke OnNodeInserted in Chrome. Is it a bug?

like image 540
Lai Yu-Hsuan Avatar asked Nov 26 '11 09:11

Lai Yu-Hsuan


1 Answers

This is from W3C

DOMNodeInserted
Fired when a node has been added as a child of another node. 
This event is dispatched after the insertion has taken place. 
The target of this event is the node being inserted.
Bubbles: Yes
Cancelable: No
Context Info: relatedNode holds the parent node

The key is the Bubbles: Yes - thats why its being fired on the container as well.

like image 104
Manse Avatar answered Sep 30 '22 12:09

Manse