Creates a copy of a node from an external document that can be inserted into the current document.
var node = document.importNode(externalNode, deep);
The new node that is imported into the document. The new node's
null, since it has not yet been inserted into the document tree.
- The node from another document to be imported.
- A boolean, indicating whether the descendants of the imported node need to be imported.
Note: In the DOM4 specification (as implemented in Gecko 13.0 (Firefox 13 / Thunderbird 13 / SeaMonkey 2.10)),
deep is an optional argument. If omitted, the method acts as if the value of
true, defaulting to using deep cloning as the default behavior. To create a shallow clone,
deep must be set to
This behavior has been changed in the latest spec, and if omitted, the method will act as if the value of
false. Though It's still optional, you should always provide the
deep argument both for backward and forward compatibility. With Gecko 28.0 (Firefox 28 / Thunderbird 28 / SeaMonkey 2.25), the console warned developers not to omit the argument. Starting with Gecko 29.0 (Firefox 29 / Thunderbird 29 / SeaMonkey 2.26)), a shallow clone is defaulted instead of a deep clone.
var iframe = document.getElementsByTagName("iframe"); var oldNode = iframe.contentDocument.getElementById("myNode"); var newNode = document.importNode(oldNode, true); document.getElementById("container").appendChild(newNode);
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
document.importNode() (or adopted using
document.adoptNode()) before they
can be inserted into the current document. For more on the
Node.ownerDocument issues, see the
W3C DOM FAQ.
Firefox doesn't currently 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.
|Feature||Chrome||Firefox (Gecko)||Internet Explorer||Opera||Safari|
|DOM 2 version||(Yes)||1.0 (1.7 or earlier)||9.0||9.0||(Yes)|
|DOM 4 version
|(Yes)||10.0 (10)||Not supported||Not supported||Nightly build|
|Feature||Android||Firefox Mobile (Gecko)||IE Mobile||Opera Mobile||Safari Mobile|
|DOM 2 version||?||1.0 (1)||?||?||?|
|DOM 4 version