JavaScript Document importNode() Method

Beginner
⏱️ 14 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance method

What You’ll Learn

document.importNode() is an instance method that copies a Node or DocumentFragment from another document so you can insert the copy into the current page later (see MDN Document: importNode()). Learn the deep flag, why parentNode stays null until insert, and how it differs from adoptNode() and cloneNode().

01

Kind

Instance method

02

Args

node, deep?

03

Returns

cloned node

04

Effect

Copies node

05

vs adopt

copy vs move

06

Status

Baseline

Introduction

Nodes from an iframe, a parsed HTML document, a detached Document, or <template>.content belong to a different document than your main page ( ownerDocument.

MDN: importNode() creates a copy of that foreign node for the calling document. The imported node is not in the tree yet — you still call appendChild() or insertBefore() to show it.

💡
Import = copy. Adopt = move.

Unlike document.adoptNode(), importNode() does not remove the original from its source document. The result is a clone owned by the current document (MDN).

⚠️
Prefer importNode for other documents (MDN)

cloneNode() clones in the source document’s context. importNode() clones in the target document’s context (CustomElementRegistry). For HTMLTemplateElement.content and other foreign documents, MDN recommends document.importNode().

Related tutorials: adoptNode(), cloneNode(), appendChild(), ownerDocument.

Understanding document.importNode()

An instance method on the Document interface (MDN). Call it on the document that will own the copy.

  • externalNode — the Node or DocumentFragment to import (MDN).
  • deep (optional) — default false; true copies the whole subtree (MDN).
  • Returns — the copied importedNode in the importing document’s scope (MDN).
  • Source unchanged — original stays where it was (MDN).
  • parentNode is null — until you insert into the tree (MDN).
  • Not inserted yet — you must still append/insert (MDN).

📝 Syntax

General forms of Document.importNode (MDN):

JavaScript
importNode(externalNode)
importNode(externalNode, deep)

Parameters

  • externalNode — the external Node or DocumentFragment to import into the current document (MDN).
  • deep Optional — boolean, default false. If true, externalNode and all descendants are copied. If false, only externalNode is imported — the new node has no children (MDN).

Return value

The copied importedNode in the scope of the importing document. Note: importedNode.parentNode is null until insertion (MDN).

MDN quick sample

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

⚡ Quick Reference

GoalCode / note
Deep copy foreign nodedocument.importNode(node, true)
Shallow copy (no children)document.importNode(node) or , false
Insert after importhost.appendChild(imported)
Before insertimported.parentNode === null (MDN)
Move instead of copydocument.adoptNode(node)
Template contentdocument.importNode(template.content, true) (MDN)
MDN statusBaseline Widely available (Jul 2015)

🔍 At a Glance

Four facts about document.importNode().

Call
document.importNode

Instance

Returns
new clone

MDN

Copies
yes

Source stays

Status
baseline

Jul 2015

📋 deep: true vs deep: false

importNode(node, true)importNode(node) / false
Root nodeCopiedCopied
DescendantsAll copied (MDN)None — new node has no children (MDN)
Typical useWidgets, templates, iframe treesCopy a single element shell
DefaultDefault is false (MDN)

Examples Gallery

Examples follow MDN Document: importNode(). Import first, then insert with appendChild.

📚 Getting Started

Copy a node from another Document and insert the clone.

Example 1 — Import from a detached Document

Create a node elsewhere, import a deep copy, then append.

JavaScript
const otherDoc = new Document();
const p = otherDoc.createElement("p");
p.textContent = "Imported paragraph";

const copy = document.importNode(p, true);
document.getElementById("host").appendChild(copy);

console.log(copy.textContent); // "Imported paragraph"
console.log(copy === p);       // false — different object
Try It Yourself

How It Works

MDN: you get a copy owned by the current document. The original p still belongs to otherDoc.

Example 2 — deep true vs false

Shallow import drops children; deep import keeps the subtree (MDN).

JavaScript
const otherDoc = document.implementation.createHTMLDocument("");
const wrap = otherDoc.createElement("div");
wrap.innerHTML = "Hi there";

const shallow = document.importNode(wrap, false);
const deep = document.importNode(wrap, true);

console.log(shallow.childNodes.length); // 0
console.log(deep.textContent);          // "Hi there"
Try It Yourself

How It Works

Default deep is false — beginners often want true for full widgets (MDN).

📈 Real Patterns

Iframes, templates, and move-vs-copy comparisons.

Example 3 — MDN: import from an iframe

Copy a node from iframe.contentWindow.document into the page.

JavaScript
const iframe = document.querySelector("iframe");
const oldNode = iframe.contentWindow.document.getElementById("myNode");
const newNode = document.importNode(oldNode, true);
document.getElementById("container").appendChild(newNode);
// Original #myNode remains inside the iframe document
Try It Yourself

How It Works

Same-origin iframes only. Cross-origin frames block contentWindow.document access.

Example 4 — Import template.content

MDN: template content is owned by a separate document — use importNode.

JavaScript
const template = document.getElementById("card-tpl");
const fragment = document.importNode(template.content, true);
document.getElementById("host").appendChild(fragment);
Try It Yourself

How It Works

Cloning with the page’s importNode uses the current document’s custom-element definitions (MDN).

Example 5 — importNode vs adoptNode

Import leaves the source; adopt removes it.

JavaScript
const otherDoc = document.implementation.createHTMLDocument("");
const a = otherDoc.createElement("span");
a.textContent = "A";
const b = otherDoc.createElement("span");
b.textContent = "B";
otherDoc.body.append(a, b);

const imported = document.importNode(a, true);
const adopted = document.adoptNode(b);

console.log({
  importSameRef: imported === a,              // false
  sourceStillHasA: otherDoc.body.contains(a), // true
  adoptSameRef: adopted === b,                // true
  sourceStillHasB: otherDoc.body.contains(b)  // false
});
Try It Yourself

How It Works

Choose importNode when both documents should keep a version of the node. Choose adoptNode() to move it.

🚀 Common Use Cases

  • Iframe widgets — copy markup from a same-origin iframe (MDN example).
  • HTML templatesdocument.importNode(template.content, true) (MDN).
  • Parsed Documents — copy nodes from DOMParser / parseHTML() results.
  • Detached Documents — clone nodes built with new Document().
  • Custom elements — clone into the target document’s registry context (MDN).
  • When you must move — use adoptNode instead of copying.

🧠 How importNode() Copies a Node

1

Locate a foreign node

From an iframe, template, or another Document (MDN).

Source
2

Call importNode on the target document

Pass deep when you need the full subtree (MDN).

Clone
3

Receive a detached clone

parentNode is null; source document still has the original (MDN).

Detached
4

Insert with appendChild / insertBefore

Only then does the copy appear in the live page tree.

📝 Notes

  • MDN: Baseline Widely available since July 2015.
  • MDN: import or adopt foreign nodes before inserting (Firefox may not enforce; still follow the rule).
  • MDN: default deep is false.
  • MDN: prefer importNode over cloneNode when cloning into another document / template content.
  • Event listeners on the original are not copied (same idea as cloneNode).
  • Related: adoptNode(), cloneNode(), appendChild().

Browser Support

Document.importNode() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Document.importNode()

Copy a Node or DocumentFragment from another document into the current document context.

Baseline Widely available
Google Chrome 1+
Yes
Mozilla Firefox 4+
Yes
Apple Safari Yes
Yes
Microsoft Edge 12+
Yes
Opera 9+
Yes
Internet Explorer 9+
Yes
importNode() Wide

Bottom line: Use document.importNode(node, true) to deep-copy foreign nodes. Prefer adoptNode when you need to move instead of copy.

Conclusion

document.importNode() is the Baseline way to copy a node from another document into your page’s document context. Remember the deep flag, insert the detached clone yourself, and choose adoptNode() when you need a move instead.

Continue with adoptNode(), moveBefore(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Pass true for deep copies when you need children (MDN)
  • Import or adopt before inserting foreign nodes (MDN)
  • Use importNode for template.content (MDN)
  • Append the returned clone to show it in the page
  • Prefer importNode over cloneNode across documents (MDN)

❌ Don’t

  • Expect the original to move — that is adoptNode
  • Forget that default deep is false
  • Assume event listeners are copied
  • Access cross-origin iframe documents
  • Skip insertion — imported nodes start detached (MDN)

Key Takeaways

Knowledge Unlocked

Five things to remember about importNode()

Copy foreign nodes into the current document context.

5
Core concepts
🔄02

deep

default false

MDN
🎯03

Detached

parentNode null

until insert
04

vs adopt

copy vs move

MDN
🛡05

Status

Baseline

2015

❓ Frequently Asked Questions

MDN: Document.importNode() creates a copy of a Node or DocumentFragment from another document, to be inserted into the current document later. The original node stays in its source document.
No. MDN marks Document.importNode() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
MDN: deep defaults to false. If true, externalNode and all descendants are copied. If false, only externalNode is imported — the new node has no children.
MDN: importedNode.parentNode is null because it has not yet been inserted into the document tree. Call appendChild or insertBefore to attach it.
importNode clones and leaves the original in place. adoptNode moves the original node out of the source document (MDN).
MDN: importNode clones in the context of the calling document; cloneNode uses the document of the node being cloned. That document context affects CustomElementRegistry for custom elements. Prefer importNode when cloning into another document (including template.content).
Did you know?

MDN highlights that <template>.content lives in a separate document. Using document.importNode(template.content, true) builds custom-element descendants with the current page’s definitions — a subtle but important reason to prefer importNode over a plain cloneNode for templates.

Next: moveBefore()

Learn how Document.moveBefore() relocates nodes while preserving DOM state.

moveBefore() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful