SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
-
Jan 6, 2008, 16:22 #1
- 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 bodyEl= document.getElementsByTagName('body')[0];
var cloneBody= bodyEl.cloneNode(true);
var htmlEl= document.getElementsByTagName('html')[0];
htmlEl.removeChild(bodyEl);
//alert(bodyEl);
//alert(cloneBody);
htmlEl.appendChild(cloneBody);
ie
PHP Code:function imgs(){
var el= document.getElementsByTagName('img');
for(var i=0,len=el.length;i<len;i++){
addEvent(el.item(i),'click',popunder);
}
}
Bye.
-
Jan 6, 2008, 16:33 #2
- Join Date
- Apr 2006
- Posts
- 802
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
event listeners are not cloned with cloneNode.
-
Jan 7, 2008, 01:25 #3
- Join Date
- May 2006
- Location
- Kakiland
- Posts
- 732
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
That's good to know
Thanks.
Bye.
-
Jan 7, 2008, 03:09 #4
- 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>
-
Jan 7, 2008, 07:44 #5
- 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.
-
Jan 7, 2008, 07:57 #6
- 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