Search w3schools.com:

SHARE THIS PAGE

Frame/IFrame contentDocument Property

Frame/IFrame Object Reference Frame/IFrame Object

Definition and Usage

The contentDocument property returns the Document object generated by a frame or iframe element.

This property can be used in the host window to access the Document object that belongs to a frame or iframe element.

Note: Because of security reasons, the contents of a document can be accessed from another document only if the two documents are located in the same domain.

Syntax

frameObject.contentDocument

or

iframeObject.contentDocument


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The contentDocument property is supported in all major browsers.

Note: Internet Explorer 8 (and higher) supports the contentDocument property only if a !DOCTYPE is specified. For earlier versions of IE, use the contentWindow property.


Example

Example

Crossbrowser example on how to change the background color of the document contained in an iframe:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script>
function changeStyle()
{
var x=document.getElementById("myframe");
var y=(x.contentWindow || x.contentDocument);
if (y.document)y=y.document;
y.body.style.backgroundColor="#0000ff";
}
</script>
</head>
<body>

<iframe id="myframe" src="demo_iframe.htm">
<p>Your browser does not support iframes.</p>
</iframe>
<br><br>

<input type="button" onclick="changeStyle()" value="Change background color">

</body>
</html>

Try it yourself »


Frame/IFrame Object Reference Frame/IFrame Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]