HTML DOM Document importNode() Method

Definition and usage

importNode() method imports nodes from another document.

The second parameter is set to true is true, the child nodes will also be imported.

When

Tip

The imported node will not be deleted from the original document.

The imported node is a copy of the original node.

See also:

document.adoptNode() method

element.cloneNode() method

Example

Import the first <h1> element from iframe (another document):
const frame = document.getElementsById("myFrame");
const h1 = frame.contentWindow.document.getElementsByTagName("H1")[0];

const node = document.importNode(h1, true);

Try it yourself

SyntaxThis method'sdocument.importNode( The parameter is a node defined in another document, and the return value is a copy of the node suitable for insertion into the document. If,

)

) Type
This method's Parameter
The parameter is a node defined in another document, and the return value is a copy of the node suitable for insertion into the document. If

Required. A node from another document.

  • Required.
  • false: Only import the node itself.

true: Import the child nodes (descendants) as well.

Return value Type
Description node

the imported node.

throws This method's If is the Document node or DocumentType node, and the method will throw an error code of NOT_SUPPORTED_ERR

DOMException exception occurs because these types of nodes cannot be imported.

Technical details This method's node The parameter is a node defined in another document, and the return value is a copy of the node suitable for insertion into the document. If deep

Only properties explicitly set in the source document will be imported when importing Element nodes. When importing Attr nodes, its specified attribute will be automatically set to true. If the value is true, then all descendant nodes of the node will also be copied. In any case, the original node and its descendant nodes will not be modified. The ownerDocument attribute of the returned copy is set to the current document, but the parentNode attribute is null because it has not been inserted into the document yet. Event listener functions registered in the original node tree will not be copied.

Browser support

document.importNode() It is a DOM Level 2 (2001) feature.

All browsers support it:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 9-11 Support Support Support Support