How many times have you navigated to a site and been instantly redirected to another page? Most often, the code used to do this is a little piece of JavaScript which is executed as soon as the page containing it loads :

location.href = "newPageName.html"

It's simple, it's effective, and it's as annoying as hell, because when you hit the 'back' button on your browser, you are taken back to the page with the redirection code in it, which of course instantly redirects to the page you're trying to escape from. Sound familiar?

Incredibly, there is an equally simple alternative which has does not have this problem associated with it, and yet for some reason it is almost never used :


Instead of directing you to another page, this replaces the page that contains it with the new page. It sounds like the same thing, but the subtle difference is that the page with the replace() code in it is not stored in the browser history. It's as if you never visited it. And so when you hit 'back', you'll go exactly where you expect to go: back to the site you were on before.

Support for location.replace() began with Netscape 3 and Internet Explorer 4, which perhaps explains some of the reluctance to use it (support for location.href began earlier, with Netscape 2 and IE 3), but it is not exactly difficult to test for compatibility and adjust accordingly.

UPDATE 2/27/03
At the time of this update, Microsoft Internet Explorer (currently at v6) and Netscape Navigator (currently at v7) remain the two most widely used browsers. The location.replace() method is implemented properly on both of those browers, and indeed on all other javascript-enabled browsers that I am aware of. And yet the use of redirect scripts which effectively disable the functioning of the "Back" button are still widespread.
Maybe, as I suggested in an earlier incarnation of this writeup, sites using these scripts operate in the hope that if a user is 'trapped' on a site, they may in the end break down and actually purchase something, simply because it's easier than navigating elsewhere. I wonder if it ever actually works?

Log in or register to write something here or to contact authors.