THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

IFrame contentDocument Property

IFrame Object Reference IFrame Object

Example

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

var x = document.getElementById("myframe");
var y = (x.contentWindow || x.contentDocument);
if (y.document)y = y.document;
y.body.style.backgroundColor = "red";
Try it Yourself »

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.


Browser Support

Property
contentDocument Yes Yes Yes Yes Yes

Syntax

iframeObject.contentDocument

Technical Details

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

More Examples

Example

Another example of how to access the document of an iframe to change the background color:

var x = document.getElementById("myframe");
var y = x.contentDocument;
y.body.style.backgroundColor = "red";
Try it Yourself »

IFrame Object Reference IFrame Object