Document.adoptNode()
transfers a node from another document into the method's document. The adopted node and its subtree is removed from its original document (if any), and its ownerDocument
is changed to the current document. The node can then be inserted into the current document.
Syntax
node = document.adoptNode(externalNode);
Parameters
node
- The adopted node that now has this document as its
ownerDocument
. The node'sparentNode
isnull
, since it has not yet been inserted into the document tree. Note thatnode
andexternalNode
are the same object after this call. externalNode
- The node from another document to be adopted.
Example
var iframe = document.querySelector('iframe'); var iframeImages = iframe.contentDocument.querySelectorAll('img'); var newParent = document.getElementById('images'); iframeImages.forEach(function(imgEl) { newParent.appendChild(document.adoptNode(imgEl)); });
Notes
Nodes from external documents should be cloned using document.importNode()
(or adopted using document.adoptNode()
) before they can be inserted into the current document. For more on the Node.ownerDocument
issues, see the W3C DOM FAQ.
Firefox doesn't currently 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.
Specification
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
adoptNode | Chrome Full support 1 | Edge Full support 12 | Firefox Full support Yes | IE Full support 9 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android ? |
Legend
- Full support
- Full support
- Compatibility unknown
- Compatibility unknown