You are here: irt.org | FAQ | JavaScript | Frame | Q317 [ previous next ]
If you want to ensure that the current document is loaded within a frameset, before a simple test to compare the top frame with the current frame, if they are the same then the document is not loaded in a frameset and you can change the location to pass the file to be loaded to another document that uses the required frameset. This is discussed in detail in the article Re-directing access within Frames #2. The following has been slightly improved over the original:
Place the following in the head section of your document:
<SCRIPT LANGUAGE="JavaScript"><!-- if (top == self) window.location.href = 'test.html' + '?' + escape('apage.html'); //--></SCRIPT>
This will pass the encoded apage.html url to the test.html document, which then loads it into the required frame:
<HTML> <SCRIPT LANGUAGE="JavaScript"><!-- document.write('<FRAMESET COLS="10%,20%,*">'); document.write('<FRAME SRC="contents.html" NAME="contents">'); document.write('<FRAME SRC="header.html" NAME="header">'); document.write('<FRAME SRC="' + (location.search ? unescape(location.search.substring(1)):'main.html') + '" NAME="main">'); document.write('<\/FRAMESET>'); //--></SCRIPT> </HTML>