Developing Web Applications

Previous Topic Next Topic

Client-Side Redirection

Redirection can also occur in client-side script. A client-side redirect shifts focus from the current page to another. It can occur as the page is loaded, or can be deferred until the user performs an action, such as clicking a button.

The browser’s object model includes a Location object, which represents the URL of the content currently displayed in the browser window. The following example uses this object to reload a frame-dependent page inside its parent frame. The script (which appears at the top of each frame page) detects that the page has been loaded as the top frame, and changes the browser window location to the parent frame. (Note that this script can be placed in an include file, to avoid duplicating it on all your pages.)

<SCRIPT LANGUAGE=JavaScript>
<!--
if(top == self) {
   var currURL = unescape(window.location.pathname);
   var newURL  = "parentFrame.asp?" + currURL;
   var appVer  = navigator.appVersion;
   var NScp = (navigator.appName == 'Netscape') &&
              ((appVer.indexOf('3') != -1) ||
               (appVer.indexOf('4') != -1));
   var MSIE = (appVer.indexOf('MSIE 4') != -1);
   if (NScp || MSIE)
      location.replace(newURL);
   else
      location.href = newURL;
}
//-->
</SCRIPT>

This method requires that the parent frame accept the child frame location as a URL parameter, and that it deal with this parameter appropriately. The following script manages the last part of this redirection:

<%@ LANGUAGE=VBScript EnableSessionState=False %>
<%
   frmSrc = Request.QueryString  'Get entire URL parameter.
   If frmSrc = "" Then frmSrc = "homepage.htm"
%>

<FRAMESET rows="60,*" frameborder=0 framespacing=0>
    <FRAME name="nav_fr"  src="navbar.htm"  scrolling=auto noresize>
    <FRAME name="page_fr" src="<%=frmSrc%>" scrolling=auto>
</FRAMESET>

© 1997-1999 Microsoft Corporation. All rights reserved.