You are here: Reference > JavaScript > client-side > HTML DOM > properties > contentWindow (frame, iframe)

contentWindow property (frame, iframe)

Browser support:
Retrieves the window generated by a frame or iframe element.
The contentWindow property can be used in the host window to access the window object that belongs to a frame or iframe element.
In that case, if you need to walk upwards in the frame window hierarchy, use the parent and top properties.
Note that because of security restrictions, the contents of a document can be accessed from another document only if the two documents are located in the same domain.
Furthermore, in Google Chrome, the contentDocument and the contentWindow.document properties cannot be used to access local files ('file:' protocol).

Syntax:

object.contentWindow;
You can find the related objects in the Supported by objects section below.
This property is read-only.

Possible values:

Reference to the window object.
Default: this property has no default value.

Example HTML code 1:

This example shows how to access the document of an iframe:
Code
frame.htm
<head>
    <script type="text/javascript">
        function SetFrameBGToRed () {
            var italicTag = document.createElement ("i");
            italicTag.innerHTML = "Italic text";

            var frame = document.getElementById ("myFrame");
            var frameDoc = frame.contentWindow.document;
            frameDoc.body.style.backgroundColor = "red";
        }
    </script>
</head>
<body>
    <button onclick="SetFrameBGToRed ()">Set frame background to red</button>
    <iframe id="myFrame" src="frame.htm" width="250px" height="250px"></iframe>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example shows how to enable content editing in an iframe element:
Code
editable.htm
<head>
    <script type="text/javascript">
        var editorDoc;
        function InitEditable () {
            var editor = document.getElementById ("editor");
            editorDoc = editor.contentWindow.document;          
            var editorBody = editorDoc.body;

                // turn off spellcheck
            if ('spellcheck' in editorBody) {    // Firefox
                editorBody.spellcheck = false;
            }

            if ('contentEditable' in editorBody) {
                    // allow contentEditable
                editorBody.contentEditable = true;
            }
            else {  // Firefox earlier than version 3
                if ('designMode' in editorDoc) {
                        // turn on designMode
                    editorDoc.designMode = "on";                
                }
            }
        }

        function ToggleBold () {
            editorDoc.execCommand ('bold', false, null);
        }
    </script>
</head>
<body onload="InitEditable ();">
    First, write and select some text in the editor.
    <br />
    <iframe id="editor" src="editable.htm"></iframe>
    <br /><br />
    You can toggle the bold/normal state of the selected text with this button:
    <br />
    <button onclick="ToggleBold ();">Bold</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content