Friendly Page Redirect - DHTML Lab - DHTML Diner | 2 | WebReference

Friendly Page Redirect - DHTML Lab - DHTML Diner | 2

DHTML Diner Logo

Friendly Page Redirect



Reason for the Blues

The diagram below illustrates a possible sequence of pages loaded into your browser that will cause the Back Button Blues:

index.html

<A HREF="coolpage.html">
See this Cool Page!</A>
-->

coolpage.html

<SCRIPT>
IE4 = document.all;
if (!IE4)
window.location = "sorry.html";
</SCRIPT>
-->

sorry.html

Assume you are on a site's start page (index.html). You click a regular HTML link to navigate to coolpage.html.

The author of coolpage.html has made the page cool only for IE users. A Navigator user, for example, will be redirected to a new page (sorry.html). The redirect is accomplished by checking for the use of IE, and executing the following statement if IE is not being used:

window.location = "sorry.html";

This is standard JavaScript syntax for loading a new page into the browser window. The following variations can also be used:

window.location.href = "sorry.html";
location = "sorry.html";
location.href = "sorry.html";
document.URL = "sorry.html";

We have used this syntax in many of our scripts here at DHTML Lab, most importanatly in the Fader and HierMenus scripts. In these scripts, the syntax is the correct one, but in the example above, it is not.

History

All of the above statements will load a new page as the next page in the browser's history. As we know, the Back and Forward buttons navigate through visited-page history. The above example creates a history stack (last-in-first-out) that looks like this:

1.sorry.html
2.coolpage.html
3.index.html

If we are viewing sorry.html, the previous entry in the browser's history is coolpage.html. Clicking the Back button takes us to coolpage.html, which has the redirect statement and so we are brought back to sorry.html. This explains the perceived looping which, of course, is the correct browser behavior!


Now, let's redirect correctly.


Produced by Peter Belesis and

All Rights Reserved. Legal Notices.
Created: Mar 21, 2000
Revised: Mar 21, 2000

URL: http://www.webreference.com/dhtml/diner/redirect/2.html