JavaScripts
Frames Required
Grizzly WebMaster home
 Grizzly WebMaster (HOME) >> JavaScripts >> Frames Required
See Also
See "Frames Buster" for techniques of breaking out of unwanted frames.
Some sites prefer to use frames to organize their web content.  Some of those sites "require" that their pages be viewed only in the context of their frames.  This set of scripts demonstrates one method of accomplishing this.

Our method uses two complementing pages, a "normal page" which represents one of your site's standard pages that requires a frames context, and a frames entry page.

Normal Page

See "Replace Window" for a more detailed explanation of the replaceWindowURL function

The "Normal page" represents any of the pages on your site that should always be viewed within a frames context.  The contained script must be placed in the <head> section of each page.

Basically the script senses whether the page is nested within "a" frame, it does not test whether the page is nested within the "correct" frame.

It should be noted that when this script executes (depending on network speed and your system speed) you may not see the non-frames page for more than a fraction of a second (if at all).

See this script in action.


<html>

<head>
<title>Frames Required Example</title>
<script language="JavaScript">
<!--
function replaceWindowURL( win, url )
{
    win.location.href = url;
}
//-->
</script>
<script language="JavaScript1.1">
<!--
function replaceWindowURL( win, url )
{
    win.location.replace( url );
}
//-->
</script>
<!--
    Please note that we define the replaceWindowURL function before
    the next block of code.  This is important since this code may
    execute "before" the page finishes loading.
-->
<script language="JavaScript">
<!--
// this file is required to be part of a frameset
// the following code will reload this page into the proper frameset
function doFramesRequired()
{
    if ( top == self )
    {
        var homeURL = "framesrequiredframe.htm";
        var thisURL = unescape(window.location.pathname);
        var url = homeURL + "?" + thisURL;

        replaceWindowURL( top, url );
    }
}

if ( top == self  &&  "MSIE" == navigator.appName )
    doFramesRequired();


//-->
</script>
</head>

<body onload="doFramesRequired()">

<p>This page must be nested in frames</p>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             </body>

</html>

Frames Entry Page

The frames entry page is setup as the default page for your data frame.  It checks the query-string of the parent-frame.  If there is a query-string it assumes that it is the name of the page that should be displayed and then switches to that page.


<html>

<head>
<title>Frames Required Entry-page</title>
<script language="JavaScript">
<!--

function doLoad()
{
    var sOption = parent.location.search;
    if ( 0 == sOption.length )
        return;

    var nPos = sOption.indexOf('?');
    if ( nPos < 0 )
        return;

    var url = unescape(sOption.substring(nPos+1,sOption.length));
    if ( 0 == url.length )
        return;

    replaceWindowURL( self, url );

}

//-->
</script>
<script language="JavaScript">
<!--
function replaceWindowURL( win, url )
{
    win.location.href = url;
}
//-->
</script>
<script language="JavaScript1.1">
<!--
function replaceWindowURL( win, url )
{
    win.location.replace( url );
}
//-->
</script>
<script language="JavaScript">
<!--
// this file is required to be part of a frameset
// the following code will reload this page into the proper frameset
    if ( top == self )
    {
        var url = "framesrequiredframe.htm";

        replaceWindowURL( top, url );
    }

//-->
</script>
</head>

<body onload="doLoad()">

<p>The contents of the &quot;normal&quot; entry page</p>

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                </body>

</html>
Service Provided by Bear Consulting Group Last modified: 25 May 2009
Copyright 2001 Bear Consulting Group