Keep Navigation Bars

If you use frames and have a rather large useful site, I'm sure you've encountered people linking to sections of your site, bypassing your navigation.  This doesn't make me happy when it happens because then I lose possible true visitors as they generally only visit that one section of the site.  I figured out a way to keep this from happening.

If you took a look at my Jewel site, you might notice that the link to my links page is simply "/jewel/links".  Thus, linking to that would let you go straight to the links page.  However, if you try and go directly to the links page, you'll notice that it mysteriously loads the navigation bar anyway.  (Well, at least if you have JavaScript turned on).

Do you have ASP capability on your server?  Yes   No    I Don't Know

Windows NT Server With JavaScript and ASP

I use JavaScript to check the page and redirect it while using ASP (Active Server Page) code to have it open the same page again.  Thus, on every page I want to protect, I put in:

<script language="JavaScript">
<!--
    var popupStr = "/jewel/reopen.asp?URL=" + escape(document.location);
    var locStr = top.location + "";
    if ((locStr.substring(0, 20) != "http://www.endor.org") || (top == self)) {
        window.open(popupStr, "_top");
    }
// -->
</script>

The JavaScript is checking for the left 20 characters to contain my domain name (yes, you must adjust this for your main URL page) in case it's a frame of someone else's window.  If it's not, then it checks to see if it's the topmost frame.  If it is, then it opens a a window to the "_top", redirecting to my ASP file.    Then, on the reopen.asp page, I place at the top of it:

<%
    URL = Request.QueryString("URL")
    If Not Len(URL)>0 Then
        URL = "main.asp"              ' The default page to open
    End If
%>

In the reopen.asp page, I place all of the same frameset that is on the main page:

<frameset framespacing="0" border="false" frameborder="0" rows="54,*">
    <frame marginwidth="0" marginheight="0" name="nav"
        scrolling="no" noresize target="contents" src="navbar.html">
    <frame name="main" target="_self" src="<%=URL%>" noresize>
</frameset>

Of course, I replace the src part of my main frame with <%=URL%> to place the URL variable in.

Any Web Page with JavaScript

On the other hand, if you don't have the capability to use ASP on your server or would rather not, you can have JavaScript detect the same thing, but then just open your main page if it doesn't follow the constraints.  For my page, the code would be as follows:

<script language="JavaScript">
<!--
    var locStr = top.location + "";
    if ((locStr.substring(0, 20) != "http://www.endor.org") || (top == self)) {
        window.open(escape("http://www.endor.org/jewel/"), "_top");
    }
// -->
</script>

The JavaScript is checking for the left 20 characters to contain my domain name (yes, you must adjust this for your main URL page) in case it's a frame of someone else's window.  If it's not, then it checks to see if it's the topmost frame.  If it is, then it opens a window to the "_top", putting the main Jewel page there.


Return to Gerrit's Nifty Utilities for the Web

©1997-1999 Gerrit Kruidhof - All Rights Reserved
Privacy Statement - webmaster@endor.org