--- title: Document.importNode() slug: Web/API/Document/importNode translation_of: Web/API/Document/importNode ---
{{APIRef("DOM")}}

Метод importNode()объекта {{domxref("Document")}} создаёт копию {{domxref("Node")}} или {{domxref("DocumentFragment")}} из другого документа, для последующей вставки в текущий документ.

Импортированный узел пока ещё не включён в дерево документов. Чтобы добавить его, вам необходимо вызвать один из методов вставки, например,  {{domxref("Node.appendChild", "appendChild()")}} или {{domxref("Node.insertBefore", "insertBefore()")}} с узлом, который находится в дереве документов.

В отличие от {{domxref("document.adoptNode()")}}, исходный узел не удаляется из исходного документа. Импортированный узел является клоном оригинала.

Синтаксис

var node = document.importNode(externalNode, deep);
node
Копируемый узел из области видимости импортируемого документа . Свойство {{domxref("Node.parentNode")}} узла = null, до тех пор, пока он не будет вставлен в дерево документа.
externalNode
Внешний Node или DocumentFragment, который импортируется в настоящий документ.
deep
Булеан, контролирующий, необходимо ли импортировать всё DOM поддерево узла  externalNode.

Note: In the DOM4 specification, deep was an optional argument with a default value of true.

This default has changed in the latest spec — the new default value is false. Though it's still an optional argument, you should always provide the deep argument for backward and forward compatibility. With Gecko 28.0 {{geckoRelease(28)}}, the console warns developers not to omit the argument. Starting with Gecko 29.0 {{geckoRelease(29)}}), a shallow clone is defaulted instead of a deep clone.

Example

var iframe = document.querySelector("iframe");
var oldNode = iframe.contentWindow.document.getElementById("myNode");
var newNode = document.importNode(oldNode, true);
document.getElementById("container").appendChild(newNode);

Notes

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

Specification Status Comment
{{SpecName("DOM WHATWG", "#dom-document-importnode", "document.importNode()")}} {{Spec2("DOM WHATWG")}}
{{SpecName("DOM2 Core", "core.html#Core-Document-importNode", "document.importNode()")}} {{Spec2("DOM2 Core")}} Initial definition

Browser compatibility

{{Compat("api.Document.importNode")}}

See also