importNode Vs createNode

J

jackwootton

Hello,

I am programmingly only for Firefox.

I have a couple (hopefully simple) questions:

1. When would I use cloneNode and when importNode? Keeping in mind
that neither importNode or cloneNode keep event handlers that have
been registered on the nodes. So I must re-register the event
handlers each time.

2. Can anyone provide their opinion on the following design decision I
made: I have a class which is provided a document object when it is
created. The document object is then used with the createNode and
createTextNode methods to create and assemble a XHTML form and sme
divs and spans to go with. I then have an array of objects that use
this class to retrieve the XHTML elements created. importNode is then
used to allow the nodes to be used in a different document.

Basically point number 2 comes down to the fact that I thought it
would be more efficient to create all the nodes a require on a single
document. And then everytime I require them subsequently, I would use
importNode, rather than creating them from scratch each time.

Bad idea or good idea?

Many thanks,,

Jack
 
M

Martin Honnen

1. When would I use cloneNode and when importNode? Keeping in mind
that neither importNode or cloneNode keep event handlers that have
been registered on the nodes. So I must re-register the event
handlers each time.

You should use importNode when you want to insert a clone of a node in
one document into another document. cloneNode suffices as long as you
have only one document.
2. Can anyone provide their opinion on the following design decision I
made: I have a class which is provided a document object when it is
created. The document object is then used with the createNode and
createTextNode methods to create and assemble a XHTML form and sme
divs and spans to go with. I then have an array of objects that use
this class to retrieve the XHTML elements created. importNode is then
used to allow the nodes to be used in a different document.

I am not sure you need a separate document, a document fragment might
suffice as a container for your nodes.
 
J

jackwootton

You should use importNode when you want to insert a clone of a node in
one document into another document. cloneNode suffices as long as you
have only one document.


I am not sure you need a separate document, a document fragment might
suffice as a container for your nodes.

Thank you for your help. Just to clarify: I should use cloneNode when
duplicating a node that belongs to a document and then appending the
new node to the same document. And I should use importNode when
duplicating a node in one document and appending it to another. Seems
logical enough, thank you.

I am slightly confused about your advice regarding my second
question. If I am to use the methods 'createNode' and
'createTextNode', then they must be executed on a specific document
e.g.: myDocument.createTextNode('my text node'). How could I create
new nodes without using a document object on which to call createNode
or createTextNode?

Assuming that I must use a document object on which to call createNode
and createTextNode, my idea was to create all the necessary DIVs and
SPANs (etc) that I require, on a single document (even though they are
not intended for use on that document) and are never actually
displayed on that document, and store them in memory. Then when I
actually require the elements that had created, I would call
'importNode' to allow them to be used on the actual document I want
them displayed on.

The thinking being that it is better to create all the elements I
require only once against a single document object, and import them
when required.
 
M

Martin Honnen

I am slightly confused about your advice regarding my second
question. If I am to use the methods 'createNode' and
'createTextNode', then they must be executed on a specific document
e.g.: myDocument.createTextNode('my text node'). How could I create
new nodes without using a document object on which to call createNode
or createTextNode?

I assume that your script is used in some document and that in the end
you want to insert nodes in the document the script is used in. So that
way you have the document object of the current window and can create a
document fragment and other nodes where the owner document is the
document in the current window.
 
J

jackwootton

I assume that your script is used in some document and that in the end
you want to insert nodes in the document the script is used in. So that
way you have the document object of the current window and can create a
document fragment and other nodes where the owner document is the
document in the current window.

Sorry, I have been an idiot and missed out some crucial information.
I am writing a firefox plugin, but the problem is a general JavaScript
one.
 
J

jackwootton

Sorry, I have been an idiot and missed out some crucial information.
I am writing a firefox plugin, but the problem is a general JavaScript
one.

The general problem being: if one has to create a lot of textNodes or
other nodes regularly, then is it more efficient to create them once
and import them into the required document, than to create them from
scratch each time?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top