...

View Full Version : cloneNode clones any object?



Kor
06-16-2006, 10:54 AM
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



<!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
06-16-2006, 02:50 PM
Or the "document's DOM tree" notion is much far extended that I thought?

KC-Luck
06-16-2006, 04:29 PM
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/author/dhtml/reference/methods/clonenode.asp

Kor
06-18-2006, 07:34 AM
I knew that. But by basis is
http://www.w3.org/TR/WD-DOM/level-one-core.html#ID-3A0ED0A4
where it says that cloneNode will return a dublicate of a Node. Nothing there about the generic objects.... Just the Node...

dumpfi
06-18-2006, 09:56 AM
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:


/*
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

Kor
06-18-2006, 12:55 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum