Javascript Reference - HTML DOM importNode() Method








The importNode() method imports a node from another document.

Browser Support

importNode Yes 9.0 Yes Yes Yes

Syntax

document.importNode(node, deep);

Parameters

Parameter Type Description
node Node Required. Node to import
deep Boolean Required. If set to false, only the node itself is imported, if set to true, all child nodes are also imported




Return Value

The imported node in Node object type.

Example

The following code shows how to return the first H1 element in an iframe.

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<script>
function myFunction()
{
    var frame=document.getElementsByTagName("iframe")[0]
    var h=frame.contentWindow.document.getElementsByTagName("h1")[0];
    
    var x=document.importNode(h,true);
    
    document.getElementById("demo").appendChild(x);
}
</script>
<iframe src="http://example.com" style="height:280px;width:420px;"></iframe>
</body>
</html>