Re: cloneNode() and events

That part of the DOM behaviour is specified in DOM-Level-2-Events. See 
the second paragraph under the "Interface EventListener" heading at 
http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventListener

"When a Node is copied using the cloneNode method the EventListeners 
attached to the source Node are not attached to the copied Node. If the 
user wishes the same EventListeners to be added to the newly created 
copy the user must add them manually."

Personally I just run any objects that I clone through an "event hookup" 
function.

Regards,

Jonathan.


M.Hompus@gac.nl wrote:
> I do a lot of j(ava)scripting with the DOM, now I've written some code that
> makes a (prototype) object that is cloned several times. This object has
> also an event attached. In Internet Explorer this event is available on
> each node that was cloned, in Mozilla this isn't happening.
> 
> When I use the same code to attach an event to a node which is not cloned
> it all just works fine. Because Mozilla normally is acting like it should,
> following the DOM standard I looked what was written about cloneNode and
> events. Surprisingly there is nothing said about event already on a node
> which is cloned.
> 
> What should be the correct action? Should events be cloned? I hope it
> should, and I think it has to be specified in the DOM(-core).
> 
> My code of the node:
> 
> // Create prototype image
> var protoImg = doc.createElement('img');
> protoImg.src = './images/zoom.gif';
> protoImg.style.width = '16px';
> protoImg.style.height = '16px';
> protoImg.style.marginLeft = '2px';
> protoImg.style.verticalAlign = 'middle';
> protoImg.alt = protoImg.title = aProperties[1];
> try {
>      // Mozilla
>      protoImg.addEventListener('click', FL_DrawZoom, false);
> } catch(e) {
>      // Internet Explorer
>      protoImg.attachEvent('onclick', FL_DrawZoom);
> }
> 
> The section of the DOM2-core about cloneNode():
> 
> cloneNode
> 
> Returns a duplicate of this node, i.e., serves as a generic copy
> constructor for nodes. The duplicate node has no parent; (parentNode is
> null.).
> Cloning an Element copies all attributes and their values, including those
> generated by the XML processor to represent defaulted attributes, but this
> method does not copy any text it contains unless it is a deep clone, since
> the text is contained in a child Text node. Cloning an Attribute directly,
> as opposed to be cloned as part of an Element cloning operation, returns a
> specified attribute (specified is true). Cloning any other type of node
> simply returns a copy of this node.
> Note that cloning an immutable subtree results in a mutable copy, but the
> children of an EntityReference clone are readonly. In addition, clones of
> unspecified Attr nodes are specified. And, cloning Document, DocumentType,
> Entity, and Notation nodes is implementation dependent.
> 
> Parameters
>      deep of type boolean
> 
>      If true, recursively clone the subtree under the specified node; if
> false, clone only the node itself (and its attributes, if it is an
> Element).
> 
> Return Value
>      Node
> 
>      The duplicate node.
> 
> 
> No Exceptions
> 
> 

Received on Wednesday, 20 March 2002 10:10:49 UTC