December 4, 1999 - Forcing Frames | WebReference

December 4, 1999 - Forcing Frames

Yehuda Shiran December 4, 1999
Forcing Frames
Tips: December 1999

Yehuda Shiran, Ph.D.
Doc JavaScript

If you've designing a frame-based Web page, you may want to make sure the child documents aren't viewed in the main window. We can use JavaScript to load the frameset document if one of the child documents is loaded in the browser window. The following expression can be used to determine if the current page isn't inside a frame:

if (self == top) {
  // current document is in the top-level window
}

If you're not familiar with the keywords self and top, be sure to read Window References (November 7, 1999). Once we have determined that we need to launch the entire frameset, we load the frameset document:

top.location.replace("parent.html");

The location.replace() method loads the frameset document without leaving the current document in the browser's history list. Our final script needs to be embedded in the document (preferably in the HEAD portion):

<SCRIPT LANGUAGE="JavaScript">
<!--
if (self == top) {
  if (parseInt(navigator.appVersion) == 2) // is the browser Navigator 2?
    location.href = "parent.html" // for Netscape Navigator 2
  else
    location.replace("parent.html");
}
// -->
</SCRIPT>

If you put this script in the HEAD portion of the document, the frameset document is called before the current document loads, so the user doesn't need to wait. Refer to Column 36, Frames and JavaScript, Part I, for a more powerful solution, and several other frame-related tips.