Previous Page
Next Page

Forcing a Page into a Frame

When a search engine catalogs one of your pages, it doesn't know that the page is part of a frame. When a user finds your page via the search engine, clicking the link shows just the single page, not the full frameset as you designed it. Script 5.3 shows the frameset. The page the user landed on (not shown) contains a call (just like the one in Script 5.1) to the JavaScript. Script 5.4 shows you how to force the full frameset to display even though Google or another search engine doesn't know about the frameset. Figure 5.4 shows the page looking out-of-place, and Figure 5.5 shows it at home, snug in its frame.

Script 5.3. The frameset page.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
     <title>This is the page to show</title>
</head>
<frameset cols="30%,70%">
     <frame src="left2.html" name="left" id="left" />
     <frame src="frame2.html" name="content" id="content" />
</frameset>
</html>

Script 5.4. Use JavaScript to force your page into the frameset.

if (top.location == self.location) {
     self.location.replace("frameset2.html");
}

Figure 5.4. A lonely page, stranded by itself.


Figure 5.5. Our page, happily reunited with its parent and sibling.


To force a page into a frame:

1.
if (top.location == self.location) {



Check to see if the current page (self) is at the top-most level. If it isn't, we're in a frameset.

2.
self.location.replace ("frameset2. html");



If the location of the current page is at the top, then replace the current page with the URL of the frameset. This then displays our framed site as we designed it.

Tip

  • This method works fine if you only have a few pages that might be called from outside their frames, because it requires a separate frameset page for every page that you might want loaded (in this example) into the content frame. The next example shows a way to handle a large site.



Previous Page
Next Page