10.2 Keeping a Page Out of the Browser History

NN 3, IE 4

10.2.1 Problem

You want to remove the current page from the browser history so that the Back button does not take the user back to the current page after a script navigates to some other page.

10.2.2 Solution

Navigate to the next page via the location.replace( ) method:


10.2.3 Discussion

The capability of keeping a page out of the browser's history can come in handy when your site contains a page that include automatic forwarding under script control. Recipe 5.10 is a typical situation that benefits from keeping a temporary page out of the history. If the user reaches the real home page, a press of the browser's Back button normally brings the user back to the temporary page, and the user is essentially trapped in an infinite loop from which escape can only occur by going forward.

You can also use this technique if you do not want a user to come back to a form after submitting it. But in this case, you must assemble the form data yourself, appending it to the URL string passed with the replace( ) method. The page returned from your CGI program replaces the form page in the browser history. Be aware, however, that this works only for form submissions that can be accomplished with the GET method, rather than the POST method. Invoking the location.replace( ) method causes the browser to request a page through a GET method just like a regular web page.

10.2.4 See Also

Recipe 5.10 for a likely scenario for using location.replace( ); Recipe 10.6 to see how to assemble form data as part of a URL.