October 17, 2000 - Promoting Child Frames | WebReference

October 17, 2000 - Promoting Child Frames

Yehuda Shiran October 17, 2000
Promoting Child Frames
Tips: October 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

When dealing with frames, you may not like your frame set to be displayed as a child frame in somebody else's frame set. You may not like the way it looks and feels, or it may function differently than what you had planned for. The remedy for this problem is to force the child frame set to become the top-level frame set. Just add the following script to the frame set file:

<SCRIPT LANGUAGE="JavaScript">
<!--
if (top.location != location) top.location.href = location.href;
// -->
</SCRIPT>

The script checks whether the current location is equal to the top level location, and assigns the local location to the top, if not.

Let's take an example. This frame set has three child frames: a tall frame on the left hand side and two short frames on the right hand side, one at the top and one at the bottom. The right top frame is not a simple frame, though. it is a frame set that is identical to the top-level frame set, so all in all we have 5 frames. But, when adding the above script, the child frame set promotes itself to be the top level frame set, sporting only three frames.

Here is the content of the five-frame file, 001016g.html above:

<HTML>
<HEAD>
<TITLE>Canvas</TITLE>
</HEAD>
<FRAMESET COLS="200, *">
   <FRAME SRC="001016a.html" NAME="leftcolumn">
   <FRAMESET ROWS="200, *">
      <FRAME SRC="001016f.html"  NAME="toprow">
      <FRAME SRC="001016c.html" NAME="bottomrow">
   </FRAMESET>
</FRAMESET>
</HTML>

And after adding the script above, we get the 3-frame file, 001016h.html:

<HTML>
<HEAD>
<TITLE>FrameSet</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
if (top.location != location) top.location.href = location.href;
// -->
</SCRIPT>
</HEAD>
<FRAMESET COLS="200, *">
   <FRAME SRC="001016a.html" NAME="leftcolumn">
   <FRAMESET ROWS="100, *">
      <FRAME SRC="001016b.html"  NAME="toprow">
      <FRAME SRC="001016c.html" NAME="bottomrow">
   </FRAMESET>
</FRAMESET>
</HTML>

Here is the code for the child frames. First, 001016a.html:

<HTML>
<HEAD>
	<TITLE>Left Tall Frame</TITLE>
</HEAD>
<BODY>
This is the left tall frame:

</BODY> </HTML>

Then 001016b.html:

<HTML>
<HEAD>
	<TITLE>Right Top Frame</TITLE>
</HEAD>
<BODY>
This is the right top frame
</BODY>
</HTML>

And finally the third one:

<HTML>
<HEAD>
	<TITLE>Right Bottom Frame</TITLE>
</HEAD>
<BODY>
This is the right bottom frame
</BODY>
</HTML>

Learn more about frames in Column 36, Frames and JavaScript, Part I, and in Column37, Frames and JavaScript, Part II.