IFrame contentDocument Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:IFrame

Description

The contentDocument property returns the Document object from an iframe element.

You can use the document reference in the host window.

The contents of a document can noly be accessed if the two documents are located in the same domain.

Return Value

A reference to the document object. If there is no document, the returned value is null

The following code shows how to change the background color of the document contained in an iframe:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<iframe id="myframe" src="http://java2s.com"></iframe>

<p id="demo"></p>

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

<script>
function myFunction() {//from w  ww  .  j a v a2 s.c  o m
    var x = document.getElementById("myframe");
    var y = x.contentDocument;
    y.body.style.backgroundColor = "red";
}
</script>

</body>
</html>

Related Tutorials