DOM:document.importNode
From MDC
Contents |
[edit] Summary
Creates a copy of a node from an external document that can be inserted into the current document.
[edit] Syntax
var node = document.importNode(externalNode, deep);
-
nodeis the new node that is imported into the document. The new node'sparentNodeisnull, since it has not yet been inserted into the document tree. -
externalNodeis the node from another document to be imported. -
deepis a boolean, indicating whether the children of the node need to be imported.
[edit] Example
var iframe = document.getElementsByTagName("iframe")[0];
var oldNode = iframe.contentDocument.getElementById("myNode");
var newNode = document.importNode(oldNode,true);
document.getElementById("container").appendChild(newNode);
[edit] Notes
The original node is not removed from the original document. The imported node is a clone of the original.
Nodes from external documents should be cloned using importNode() (or adopted using adoptNode()) before they can be inserted into the current document. For more on the ownerDocument issues see the W3C DOM FAQ.
Firefox does not presently enforce this rule (it did for a while during the development of Firefox 3, but too many sites break when this rule is enforced). We encourage web developers to fix their code to follow this rule for improved future compatibility.