English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM importNode() Method

HTML DOM Document Object

importNode()method creates a copy of a node from another document.

If the second parameter is set to true, the child nodes (descendants) of the imported node will also be imported.

The imported node is not yet included in the document tree. To include it, you need to call an insertion method, such aselement.appendChild()orelement.insertBefore()with the nodes currently in the document tree.

withdocument.adoptNode()differently, the original node will not be deleted from its original document.

You can also useelement.cloneNode()The method copies nodes from the current document without deleting them.

Syntax:

document.importNode(externalNode, deep)
var iframe = document.querySelector('iframe');
var iframeImages = iframe.contentDocument.querySelectorAll('h2');
var newParent = document.getElementById('result');
iframeImages.forEach(function(elem) {
newParent.appendChild(document.importNode(elem, true));
});
Test and see‹/›

Browser Compatibility

All browsers fully support the importNode() method:

Method
importNode()isisisisis

Parameter Value

ParameterDescription
externalNodeThe node imported from another document
deepA boolean value specifying whether to importexternalNodeThe entire DOM subtree:
  • If the value ofdeepSet totruethen copyexternalNodeand all its descendants

  • If the value ofdeepSet tofalseif false, then only importexternalNode

Technical Details

Return Value:Represents the Node object of the imported node
DOM Version:DOM 2Level

HTML DOM Document Object