Document importNode() Method - Javascript DOM

Javascript examples for DOM:Document importNode

Description

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

The original node is not removed from the other document.

The imported node is a copy of the original.

Syntax

document.importNode(node, deep);

Parameter Values

Parameter TypeDescription
node? Node object Required. The node from another document.
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:

A Node object, representing the imported node

The following code shows how to return the first <h1> element that appears in an iframe (another document):

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<iframe src="http://java2s.com" style="height:380px;width:520px;"></iframe>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {/* w ww .  j  av a 2  s. c o m*/
    var frame = document.getElementsByTagName("IFRAME")[0]
    var h = frame.contentWindow.document.getElementsByTagName("H1")[0];
    var x = document.importNode(h, true);
    document.body.appendChild(x);
}
</script>

</body>
</html>

Related Tutorials