importNode Vs createNode

Discussion in 'Javascript' started by jackwootton@gmail.com, Jul 15, 2007.

  1. Guest

    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
     
    , Jul 15, 2007
    #1
    1. Advertising

  2. wrote:

    > 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.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jul 15, 2007
    #2
    1. Advertising

  3. Guest

    On Jul 15, 6:57 am, Martin Honnen <> wrote:
    > wrote:
    > > 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.
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/


    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.
     
    , Jul 15, 2007
    #3
  4. wrote:

    > 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.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jul 15, 2007
    #4
  5. Guest

    On Jul 15, 9:14 am, Martin Honnen <> wrote:
    > wrote:
    > > 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.
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/


    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.
     
    , Jul 15, 2007
    #5
  6. Guest

    On Jul 15, 9:34 am, "" <>
    wrote:
    > On Jul 15, 9:14 am, Martin Honnen <> wrote:
    >
    >
    >
    > > wrote:
    > > > 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.

    >
    > > --

    >
    > > Martin Honnen
    > > http://JavaScript.FAQTs.com/

    >
    > 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?
     
    , Jul 15, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish

    performance of importNode

    Andy Fish, Nov 4, 2003, in forum: XML
    Replies:
    0
    Views:
    662
    Andy Fish
    Nov 4, 2003
  2. Replies:
    3
    Views:
    1,055
    Martin Honnen
    Dec 20, 2006
  3. Simon Brooke
    Replies:
    10
    Views:
    1,150
    Joe Kesselman
    Mar 17, 2007
  4. yawnmoth

    unable to get importNode working

    yawnmoth, Aug 22, 2006, in forum: Javascript
    Replies:
    5
    Views:
    129
    Martin Honnen
    Aug 23, 2006
  5. Une Bévue

    importNode and root node name

    Une Bévue, Apr 26, 2008, in forum: Javascript
    Replies:
    11
    Views:
    238
    Une Bévue
    Apr 26, 2008
Loading...

Share This Page