Email This Post Email This Post Print This Post Print This Post

Replacing History

Mon, Oct 15, 2007 – 4:41 am

How to prevent a page from being placed into a web browsers history.

There are a lot of times when you want to disable a browsers “Back” button or delete items from a browsers history list.

Unfortunately there is no function that lets you delete items from history, but you can use the location.replace method to prevent the page from ever showing up in history to begin with.

location.replace(URL) – …loads the specified URL over the current history entry. After calling the replace method, the user cannot navigate to the previous URL by using browser’s Back button

Link: http://docs.sun.com/source/816-6408-10/location.htm#1194240

Example 1:

<script language="JavaScript">
  function ClickMe() {
    location.replace('http://www.yahoo.com');
  }
</script>

If the function ClickMe() was called in response to the user clicking on a button or hyper link then they would be taken to the www.yahoo.com web page AND the page they just left would *not* be in the history list. If they tried to use the Back button to return they would not return to it, but to the previous one.

Example 2:

<script language="JavaScript">
    location.replace('http://www.yahoo.com');
</script>

If the above script was placed between the <head> and </head> tags then the user would automatically be taken to the www.yahoo.com web page without ever having the current page displayed and without having it ever entered into the history list (useful when used on a page which does server side inserts to a database in response to input from a form – it prevents the user from hitting the “Back” button and accidently inserting duplicates)

  1. 2 Responses to “Replacing History”

  2. i tried the first method but it does not work

    By prajakta on Apr 23, 2008

  3. It could be a factor of looking at your page prior to putting the function into place.

    Exit your browser and try again now that the function is in place.

    By Stan Slaughter on Apr 24, 2008

Post a Comment