Frames are a way to organize and structure HTML documents, creating compound views that the user sees within the main browser window. Each frame is itself a unique window within the main window, and having its own window object.
Frames are created by using the IFRAME element for inline frames, or the FRAMESET and FRAME elements for multiple view windows. For examples of creating frames, see Frames and Framesets in the authoring section.
In a document within a frame, such as the x.htm and y.htm documents in the example above, you can use the window object as you would in a document within the main window. This means you can get access to the document and event objects for the frameset document and write script that will run from this frameset document.
Note that if you use script, or need to instantiate an OBJECT from this frameset document, you need to define these in the HEAD section. Also note that a BODY element is mutually exclusive from a FRAMESET.
Using both in a single document will result in the first one being instantiated and the second one ignored. Many authors try to use a BODY element to set the background color, and then instantiate a FRAMESET. Internet Explorer 4.0 will instantiate the BODY, and ignore the FRAMESET. Move the background color settings to the FRAMESET, and remove the BODY from this document. The browser will then instantiate the FRAMESET as you would expect.
You can also gain access to the window that created the frame ( called the parent window ) and to the other frames created by the parent by using the parent property and the frames collection. The following JScript™ ( compatible with ECMA 262 language specification ) example uses these to display the URL of the documents in each frame.
for ( i=0; i < window.parent.frames.length; i++ )
alert ( "Frame #" + i + " contains: " +
window.parent.frames ( i ).location );
The frames collection contains window objects for each frame, so you can use the same properties and methods with these objects as you would within the current window. For example, you can gain access to the complete contents of the document in another frame by using frames to display the title of each document, as in the following example:
for ( i=0; i < window.parent.frames.length; i++ )
alert ( "The title of frame #" + i + " is: " +
window.parent.frames ( i ).document.title );
Note that cross-frame security limits scripting to documents loaded from the same domain. Use the domain property to determine and/or set the domain of a document. For more information on cross-frame security and scripting considerations, see Cross-Frame Scripting and Security.
The frames collection also works this way for the document object. In this case, the frames collection from the document contains window objects for all frames created by IFRAME elements in a document. Consider the following document:
<html>
<head>
<title>Two Floating Frames</title>
<script language="JavaScript">
<!--
function showFrames ( ) {
for ( i=0; i < window.document.frames.length; i++ )
alert ( "The title of frame #" + i + " is: " +
window.document.frames ( i ).document.title );
}
}
//>
</script>
</head>
<body onload="showFrames ( )">
<iframe src="x.htm" align=left></iframe>
Here's some text to the right of a frame.
<br clear=left>Here's some text beneath the frame.<br>
<iframe src="y.htm" align=right></iframe>
Here's some text to the left of a frame.
<br clear=right>Here's some text beneath the frame.
</body>
</html>
Show me
The document above contains two floating frames, one aligned to the left and the other to the right. Once the document is loaded, the showFrames function uses the frames collection to access each frame and display the title of the corresponding document.
Unlike windows created using the open method, you cannot close the window associated with a frame. The frame is "closed" when the window that created it is unloaded. Also, properties that apply to the main window, such as status, have no purpose within a frame. Frames do not have status bars, so attempting to get or set status has no effect for the individual frames, and you should access the parent frameset document to set the status bar.
Notice that accessing the window object associated with a frame is not the same as accessing the FRAME or IFRAME element that created the frame. In particular, you cannot access the attributes of these elements by using the window object. Instead, you must use the object for the element, which you can get from the all collection for the document that contains the FRAME element. For example, if you want to remove the scroll bars from a frame, you have to set the scrolling property of the IFRAME element as follows:
document.all.tags ( "IFRAME" ).item ( 1 ).scrolling = "no";
For more information about element objects and their properties, see Scripting with Elements and Collections.