W3cubDocs

/DOM

document.importNode

The Document method importNode() creates a new copy of the specified Node or DocumentFragment from another document so that it can be inserted into the current Document. It is not yet included in the document tree; to do that, you need to call a method such as appendChild() or insertBefore().

Syntax

var node = document.importNode(externalNode, deep);
externalNode
The new Node or DocumentFragment to import into the current document. After importing, the new node's parentNode is null, since it has not yet been inserted into the document tree.
deep
A Boolean value which indicates whether or not to import the entire DOM subtree originating at externalNode. If this parameter is true, then externalNode and all of its descendants are copied; if false, then only the single node, externalNode, is imported.

In the DOM4 specification, deep is listed as an optional argument. If omitted, the method acts as if the value of deep was true, defaulting to using deep cloning as the default behavior. To create a shallow clone, deep must be set to false.

This behavior has been changed in the latest spec, and if omitted, the method will act as if the value of deep was 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 / Firefox OS 1.3), 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.

Example

var iframe = document.getElementsByTagName("iframe")[0];
var oldNode = iframe.contentWindow.document.getElementById("myNode");
var newNode = document.importNode(oldNode, true);
document.getElementById("container").appendChild(newNode);

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 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.

Specifications

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
DOM 2 version (Yes) (Yes) 1.0 (1.7 or earlier) 9.0 9.0 (Yes)
DOM 4 version
(deep optional)
(Yes) (Yes) 10 (10) No support No support Nightly build
Feature Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
DOM 2 version ? (Yes) 1.0 (1) ? ? ?
DOM 4 version
(deep optional)
? No support 10.0 (10) ? ? ?

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/document/importNode