| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/Web/API/Document/importNode | [Back] [Original] |
Get to know MDN better
This feature is well established and works across many devices and browser versions. Its been available across browsers since July 2015.
The importNode() method of the Document interface creates a copy of a Node or DocumentFragment from another document, to be inserted into the current document later.
The imported node is not yet included in the document tree. To include it, you need to call an insertion method such as appendChild() or insertBefore() with a node that is currently in the document tree.
Unlike document.adoptNode(), the original node is not removed from its original document. The imported node is a clone of the original.
The Node.cloneNode() method also creates a copy of a node. The difference is that importNode() clones the node in the context of the calling document, whereas cloneNode() uses the document of the node being cloned. The document context determines the CustomElementRegistry for constructing any custom elements. For this reason, to clone nodes to be used in another document, use importNode() on the target document. The HTMLTemplateElement.content is owned by a separate document, so it should also be cloned using document.importNode() so that custom element descendants are constructed using the definitions in the current document. See the Node.cloneNode() page's examples for more details.
importNode(externalNode)
importNode(externalNode, deep)
externalNodeThe external Node or DocumentFragment to import into
the current document.
deep OptionalA boolean flag, whose default value is false,
which controls whether to include the entire DOM subtree
of the externalNode in the import.
deep is set to true, then
externalNode and all of its descendants are copied.deep is set to false, then only
externalNode is imported the new node has no children.The copied importedNode in the scope of the importing document.
Note:
importedNode's Node.parentNode is null, since it has not yet been inserted into the document tree!
const iframe = document.querySelector("iframe");
const oldNode = iframe.contentWindow.document.getElementById("myNode");
const newNode = document.importNode(oldNode, true);
document.getElementById("container").appendChild(newNode);
Before they can be inserted into the current document, nodes from external documents should either be:
document.importNode(); ordocument.adoptNode().Note: Although Firefox doesn't currently enforce this rule, we encourage you to follow this rule for improved future compatibility.
For more on the Node.ownerDocument issues, see the W3C DOM FAQ.
| Specification |
|---|
| DOM # ref-for-dom-document-importnode |
document.adoptNode(), which behaves very similar to this methodNode.appendChild()Node.insertBefore()This page was last modified on Dec 1, 2025 by MDN contributors.
DocumentactiveElementactiveViewTransitionadoptedStyleSheetsalinkColorallanchorsappletsbgColorbodycharacterSetchildElementCountchildrencompatModecontentTypecookiecurrentScriptcustomElementRegistrydefaultViewdesignModedirdoctypedocumentElementdocumentURIdomainembedsfeaturePolicyfgColorfirstElementChildfontsformsfragmentDirectivefullscreenfullscreenElementfullscreenEnabledheadhiddenimagesimplementationlastElementChildlastModifiedlastStyleSheetSetlinkColorlinkslocationpictureInPictureElementpictureInPictureEnabledpluginspointerLockElementpreferredStyleSheetSetprerenderingreadyStatereferrerrootElementscriptsscrollingElementselectedStyleSheetSetstyleSheetsstyleSheetSetstimelinetitleURLvisibilityStatevlinkColorxmlEncodingxmlVersionadoptNode()append()ariaNotify()browsingTopics()caretPositionFromPoint()caretRangeFromPoint()clear()close()createAttribute()createAttributeNS()createCDATASection()createComment()createDocumentFragment()createElement()createElementNS()createEvent()createExpression()createNodeIterator()createNSResolver()createProcessingInstruction()createRange()createTextNode()createTouch()createTouchList()createTreeWalker()elementFromPoint()elementsFromPoint()enableStyleSheetsForSet()evaluate()execCommand()exitFullscreen()exitPictureInPicture()exitPointerLock()getAnimations()getElementById()getElementsByClassName()getElementsByName()getElementsByTagName()getElementsByTagNameNS()getSelection()hasFocus()hasPrivateToken()hasRedemptionRecord()hasStorageAccess()hasUnpartitionedCookieAccess()importNode()moveBefore()mozSetImageElement()open()prepend()queryCommandEnabled()queryCommandState()queryCommandSupported()querySelector()querySelectorAll()releaseCapture()replaceChildren()requestStorageAccess()requestStorageAccessFor()startViewTransition()write()writeln()AbortControllerAbortSignalAbstractRangeAttrCDATASectionCharacterDataCommentCustomEventDOMErrorDOMExceptionDOMImplementationDOMParserDOMTokenListDocumentFragmentDocumentTypeElementEventEventTargetHTMLCollectionMutationObserverMutationRecordNamedNodeMapNodeNodeIteratorNodeListProcessingInstructionQuotaExceededErrorRangeShadowRootStaticRangeTextTreeWalkerXMLDocumentXPathEvaluatorXPathExpressionXPathResultXSLTProcessorYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |