Visit Mozilla.org

DOM:document.importNode

From MDC

« Gecko DOM Reference

Contents

[edit] Summary

Creates a copy of a node from an external document that can be inserted into the current document.

[edit] Syntax

var node = document.importNode(externalNode, deep);
  • node is the new node that is imported into the document. The new node's parentNode is null, since it has not yet been inserted into the document tree.
  • externalNode is the node from another document to be imported.
  • deep is a boolean, indicating whether the children of the node need to be imported.

[edit] Example

var iframe = document.getElementsByTagName("iframe")[0];
var oldNode = iframe.contentDocument.getElementById("myNode");
var newNode = document.importNode(oldNode,true);
document.getElementById("container").appendChild(newNode);

[edit] Notes

The original node is not removed from the original document. The imported node is a clone of the original.

Nodes from external documents should be cloned using importNode() (or adopted using adoptNode()) before they can be inserted into the current document. For more on the ownerDocument issues see the W3C DOM FAQ.

Firefox does not presently enforce this rule (it did for a while during the development of Firefox 3, but too many sites break when this rule is enforced). We encourage web developers to fix their code to follow this rule for improved future compatibility.


[edit] Specification

DOM Level 2 Core: Document.importNode