Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q991 Can I sniff the browsers history and then redirect the browser depending on where they have been before?

irt.org | Knowledge Base | JavaScript | History | Q991 [ previous next ]

Q991 Can I sniff the browsers history and then redirect the browser depending on where they have been before?

Use the documents referrer property:

<script language="JavaScript"><!-- 
if (document.referrer.indexOf('page2.htm') > -1)
    location.href = 'page2.htm';
else if (document.referrer.indexOf('page3.htm') > -1)
    location.href = 'page2.htm';
else
    location.href = 'default.htm';
//--></script>

The following can only be done in Netscape Navigator 4+, but works on previous pages and not just the last:

<script language="JavaScript"><!-- 
if (document.layers) {
    netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
    var nextUrl = '';
    for (var i=window.history.length-1;i>-1;i--) {
        if (window.history[i].indexOf('irt.org') > -1) {
            nextUrl = 'http://www.irt.org/script/faq.htm';
            break;
        }   
        if (window.history[i].indexOf('netscape.com') > -1) {
            nextUrl = 'http://developer.netscape.com/';
            break;
        }   
    }
    if (nextUrl != '')
        location.href = nextUrl;
}
//--></script>

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.