Example 1 — Import a Node and Append It
Create a paragraph in another document, import a deep copy, then append it.
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) How It Works
- Make another document.
createHTMLDocumentgives a separate HTML document. - Create and attach the node there. Put the paragraph in
other.body. - Import a copy.
document.importNode(p, true)returns a new node owned bydocument. - Append the copy. The page shows the copy;
other.bodystill contains the original.

