JavaScript Document importNode() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

document.importNode() copies a node (or DocumentFragment) from another document into the current document. The original stays put. The copy is not in the tree yet — insert it with appendChild / append / insertBefore.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call document.importNode. Start with this line.

JavaScript
document.importNode(externalNode, deep?)
CallWhat it does
document.importNode(externalNode, deep?)Primary signature.
document.importNode(...)Call with the arguments your case needs.

Built into the browser. Call importNode(...) on document — no import required.

Library declarations
JavaScript
document.importNode(externalNode, deep?)

Parameters

ParameterDescription
externalNodeRequired. Node or DocumentFragment to copy
deepOptional. Include descendants when true
Insert stepRequired. Import alone does not attach it to the tree

Return Value

Return ValueDescription
*New copy owned by this document

JavaScript Examples

Example 1 — Import a Node and Append It

Create a paragraph in another document, import a deep copy, then append it.

JavaScript
const other = document.implementation.createHTMLDocument("demo");
const p = other.createElement("p");
p.textContent = "Imported paragraph";
other.body.appendChild(p);

const copy = document.importNode(p, true);
document.body.appendChild(copy);

console.log(copy.textContent);
// "Imported paragraph"
console.log(copy === p);
// false  (new object)
console.log(other.body.contains(p));
// true   (original stays)
Try It Yourself

How It Works

  1. Make another document. createHTMLDocument gives a separate HTML document.
  2. Create and attach the node there. Put the paragraph in other.body.
  3. Import a copy. document.importNode(p, true) returns a new node owned by document.
  4. Append the copy. The page shows the copy; other.body still contains the original.

Example 2 — deep True vs False

Shallow import copies the element only; deep import includes children.

JavaScript
const other = document.implementation.createHTMLDocument("demo");
const ul = other.createElement("ul");
const li = other.createElement("li");
li.textContent = "Item";
ul.appendChild(li);

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

console.log(shallow.childElementCount); // 0
console.log(deep.childElementCount);    // 1
console.log(deep.firstChild.textContent); // "Item"
Try It Yourself

How It Works

  1. Build a small tree. A ul with one li child.
  2. Shallow import. deep = false (MDN default) copies only ul → no children.
  3. Deep import. deep = true copies ul and descendants.
  4. Rule of thumb. Need the subtree? Pass true.

Example 3 — importNode() vs adoptNode()

Import clones; adopt moves. The source document shows the difference.

JavaScript
const other = document.implementation.createHTMLDocument("demo");
const a = other.createElement("p");
a.textContent = "A";
other.body.appendChild(a);

const b = other.createElement("p");
b.textContent = "B";
other.body.appendChild(b);

document.importNode(a, true);  // copies A
document.adoptNode(b);         // moves B

console.log(other.body.contains(a)); // true
console.log(other.body.contains(b)); // false
Try It Yourself

How It Works

  1. Put two paragraphs in other. Both start in the source body.
  2. Import A. A clone is made; original A stays → contains(a) is true.
  3. Adopt B. The original leaves other.body → contains(b) is false.
  4. Choose wisely. Keep the source with import; move with adopt.

Browser Support

Document.importNode() is a browser DOM API marked Baseline Widely available on MDN (since July 2015). It works in every modern browser — no polyfill needed for ordinary web apps. See the MDN importNode() reference.

✓ Baseline Widely available

Document.importNode()

Supported in Chrome, Firefox, Safari, Edge, Opera, and Internet Explorer 9+.

Baseline Widely available
Google Chrome All modern versions
Yes
Microsoft Edge All modern versions
Yes
Mozilla Firefox All modern versions
Yes
Apple Safari All modern versions
Yes
Opera All modern versions
Yes
Internet Explorer IE 9+
Yes
importNode() Excellent in browsers

Bottom line: Safe to use in browsers. Remember deep defaults to false, and always insert the copy after import.

Frequently Asked Questions

It creates a copy of a Node or DocumentFragment from another document so you can insert that copy into the current document later. The original node stays in its source document (MDN).
No. MDN marks Document.importNode() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
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).
The imported copy is not in the document tree yet. Call appendChild, append, or insertBefore to attach it (MDN).
importNode() clones and leaves the original in place. adoptNode() moves the original node out of the source document (MDN).
importNode() clones in the context of the calling document; cloneNode() uses the document of the node being cloned. That context matters for custom elements and for HTMLTemplateElement.content — prefer importNode() when cloning into another document (MDN).

Next: moveBefore()

Learn how to move a node before another child without removing and re-inserting manually.

moveBefore() tutorial →

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.

16 people found this page helpful