Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts

    cloneNode clones any object?

    The following is rather an assumption than a question.

    Trying to find a way to have an initial genuine clone ready to be appended for many times whithin the same session I have discovered that the cloneNode() method is able to clone even an object which is not part of the DOM tree (or at list I believe so). In other words cloneNode is able to clone a clone

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    meta http-equiv="Content-Style-Type" content="text/css">
    <
    meta http-equiv="Content-Script-Type" content="text/javascript">
    <
    script type="text/javascript">
    var 
    cloned
    onload 
    = function(){
    cloned=document.getElementById('txt').cloneNode(true)
    }
    function 
    addC(){
    var 
    clonedclone=window['cloned'].cloneNode(true)
    document.getElementsByTagName('body')[0].appendChild(clonedclone)
    }
    </script>
    </head>
    <body>
    <input type="button" value="ADD" onclick="addC()">
    <input type="text" id="txt" value="one">
    </body>
    </html> 
    This is puzzleing me a little, as a clone (see window['cloned']) is in fact a window's object (or a function's object, if the variable is local) not a document's DOM tree object. My "academical" question is: Are my above presumptions correct? Can this be extended to other type of objects?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Or the "document's DOM tree" notion is much far extended that I thought?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Regular Coder
    Join Date
    Aug 2005
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    cloneNode does just that it stuffs away a "copy" of that element,
    you are setting that "in-memory" DOM Element to a variable of window scope, then you can still clone the "in-memory" Node to finally append to the Document.
    http://msdn.microsoft.com/workshop/a.../clonenode.asp

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I knew that. But by basis is
    http://www.w3.org/TR/WD-DOM/level-on...ml#ID-3A0ED0A4
    where it says that cloneNode will return a dublicate of a Node. Nothing there about the generic objects.... Just the Node...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Well, it does not create a generic object, it just creates a duplicate node.

    And wether you add the cloned Node as a property to the window or any other object does not make a difference. It's still a Node object.

    See:
    Code:
    /*
        this var will hold our cloned node
        and because it's declared as a global variable we can access it as `window['cloned']` (all global variables and functions are infact properties / methods of the window object)
    */
    var cloned = null;
    
    window.onload = function()
    { 
        // we store the cloned node as the content of our variable `cloned` resp. `window['cloned']`
        cloned = document.getElementById('txt').cloneNode(true);
    }
    
    function addC()
    { 
        // we invoke the `cloneNode` method on our cloned node, thus creating a clone of the already cloned node, and assign it to the local variable `clonedclone`
        var clonedclone = window['cloned'].cloneNode(true);
        // finally we append the clone of the cloned node to the document
        document.getElementsByTagName('body')[0].appendChild(clonedclone);
    }
    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by dumpfi
    Well, it does not create a generic object, it just creates a duplicate node.

    And wether you add the cloned Node as a property to the window or any other object does not make a difference. It's still a Node object.

    See:
    I guessed so as well, tx everyone
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •