How can I maintain the layout of my frames?

When you create a page that uses frames, you probably don't want people to load your pages outside this layout.
For example, in Navigator, you can right-click in a frame and select "Open Frame in New Window". This code will prevent someone from doing that. It will also prevent someone from linking directly to that page, so that you can maintain the integrity of the frame layout you created.

This script checks if a page is inside the correct frame, and loads the page with the correct frameset if it's not.

Source
<SCRIPT language="JavaScript">
if (window.name != "framename") {
window.location = "index.html";
}
</SCRIPT>

You must put this script in the head section of the page that you want to protect. Change framename to the name of the frame where the page should be loaded. And change index.html to the URI of the page that contains the frameset.

You can extend this technique and make the particular page, that was loaded outside the correct frame, appear inside the frame. For that to work the script must pass its own name to the page with the frameset :

Source
<SCRIPT language="JavaScript">
cPage = location.pathname
if (cPage.indexOf('/') != -1) {
cPage = cPage.substring(cPage.lastIndexOf('/') + 1,cPage.length)
}
if (window.name != "framename") {
window.location = "index.html?" + cPage;
}
</SCRIPT>

The page with the frameset has to be changed so that the page is written dynamically, using the passed parameter.

The next script sets up two frames, and loads the protected page in the lower frame :

Source
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
cPage = location.search.substring(1,location.search.length)
if (cPage == '') {
cPage = 'default.html'
}
cFrame = '<HTML><HEAD></HEAD>'
cFrame += '<FRAMESET rows="50,*">'
cFrame += '<FRAME src="frame1.html">'
cFrame += '<FRAME name="framename" src="' + cPage + '">'
cFrame += '</FRAMESET>'
cFrame += '</HTML>'
document.write(cFrame)
</SCRIPT>
</HEAD>
</HTML>

Change the frameset layout to the one you need, but the complete page must be defined inside the script.

If this frame is loaded in the normal way then the file default.html is loaded. Change the value of cPage if you want to use another file.

Back to the FAQ main page
Statistics

  Copyright © 1996 - 2000 Rob Schlüter,   schluter@knoware.nl   (last updated 1999/03/01)