SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    body.cloneNode get rid of js code ....

    Hi.
    I'm wondering why on earth if
    I try a thing like this:

    PHP Code:
    var bodyEldocument.getElementsByTagName('body')[0];
    var 
    cloneBodybodyEl.cloneNode(true);
    var 
    htmlEldocument.getElementsByTagName('html')[0];
    htmlEl.removeChild(bodyEl);
    //alert(bodyEl);
    //alert(cloneBody);
    htmlEl.appendChild(cloneBody); 
    all the events and their handlers disappear
    ie
    PHP Code:
    function imgs(){
            var 
    eldocument.getElementsByTagName('img');
            for(var 
    i=0,len=el.length;i<len;i++){
                
    addEvent(el.item(i),'click',popunder);
            }
        } 

    Bye.

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    event listeners are not cloned with cloneNode.

  3. #3
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's good to know
    Thanks.
    Bye.

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The attached events are copied:
    Code:
    <p onclick="alert('clicked');">click me</p>
    <script type="text/javascript">
    var p = document.getElementsByTagName("p")[0];
    // clone the paragraph
    var newP = p.cloneNode(true);
    // add clone to document.body
    document.body.appendChild(newP);
    </script>
    When are you adding the events?

  5. #5
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I should have said, listeners added with addEventListener are not cloned.
    The logic of this is that cloning an element and replacing the original with the clone is sometimes the only way to remove anonymous listeners from the element.

  6. #6
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, events attached with addEventListner are not cloned. But... sadly, they are in IE when using attachEvent.



Bookmarks

Posting Permissions

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