Click to See Complete Forum and Search --> : Frames


AWhiteC
02-26-2003, 08:05 AM
When a sub-document of a frameset is accessed directly (e.g. from a search engine), it appears without the context of the surrounding frameset. To restore the frameset context, I'm use the following script:

<SCRIPT TYPE="text/javascript">
<!--
if (window.location.href == top.location.href) {
if (window.location.replace)
window.location.replace('frameset.html');
else
// causes problems with back button, but works
window.location.href = 'frameset.html';
}
//-->
</SCRIPT>

However, it seems to cause the initial frameset to be shown, whereas I want it to show the frameset with the requested sub-document embedded therein.

This must be something of a FAQ, for frames enthusiasts at least.

Any ideas?

AWhiteC
02-26-2003, 10:53 AM
Replying to my own post ...

I've found the answer myself ...

I put this in the sub-document :-

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
<!--
if (window.location.href == top.location.href)
{
if (window.location.replace)
// best solution, but may not be supported
window.location.replace('index.htm?' + window.location.href);
else
// causes problems with back button, but works
window.location.href = 'index.htm?' + window.location.href;
}
//-->
</SCRIPT>

... and this in the frameset document :-

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
<!--
var first = true;

// Function to see if HTML document has been passed in as parameter
// ("search string"), and if so to put in main frame
function initframe()
{
if (first)
{
first = false;
if (window.location.search != '')
window.frames[2].location = window.location.search.substr(1);
}
}
//-->
</SCRIPT>

... then put this where the frame is defined in the frameset document :-

<FRAME SRC="Home.htm" NAME="Main" ONLOAD="initframe()">

... I get what I want!

Worth FAQing?