There is no guaranteed way of simulating a back button in HTML due to the vagaries of what a back button truly does. However, it is possible to safely simulate such a thing on some browsers when done properly. In a similar vein, and what is often meant when such a questions is asked, it is a fairly simple thing to create a link to the previous page.
When viewed in its simplest light, a back button is merely a means of returning a user to the previously visited page. Often, when someone asks how to simulate a back button, they really mean to ask how to create a link that points to the previously visited page. While perhaps not apparent, there is a difference between the two.
Most browsers tend to keep a list of which websites the user has visited and in what order they have done so. It is via this list that the browser knows which site to go to when the user presses the back or forward button. You can think of this list as a sort of timeline of pages that the user has visited with the back and forward buttons allowing the user to travel back a forth on this timeline. Finally, the browser keeps a pointer on where on this timeline the user currently sits.
When a user clicks on a link, the browser tends to discard those sites that that are forward of this pointer. It then adds the new URL to the front of the list and points the pointer at this new location. When a user clicks on the back button, the browser points the pointer to the location in the list immediately prior to the current location. When a user clicks on the forward button, the browser moves the pointer to the location immediately after the current location. An example follows:
|CURRENT ACTION||LIST (current location is in bold)|
|link to B||AB|
|link to C||ABC|
|link to B||ABCB|
As mentioned above, there is a distinct difference between using a browser's back button and clicking on a link that takes the user to the previously visited page. In the above example, pressing the back button merely moved the pointer to the previous location. However, clicking on a link that points to the previous page adds a new item to the history list.
Adding a link to the previously page is not at all difficult with the aid of CGI programming. In general, a browser usually informs the server which page it is coming from. This is done via the CGI variable HTTP_REFERER. A CGI program could take this information and dynamically add it to the current page.
There is a caveat though: A browser does not have to send the HTTP_REFERER information nor should it in some cases. For instance, if a site is reached via a user's bookmarks, there is no referring page. The same applies to a case where the user entered the URL manually.
For those who do not have CGI at their disposal or for those who do not wish to deal with programming, there is another method that can be used; the "best guess" method. More often then not, an author can accurately predict what page the user will have come from.
Most websites are structured in nature. There is usually a single homepage which may lead to section indices, which lead to subsection indices, which lead to the content pages. In these cases, it is usually a simple task to figure out which page the user was previously visiting. This method is not guarateed by any means, but it is a fairly safe bet.
<A HREF="previous.html" onClick="history.back();return false;">Go back</A>
|[Section Index]||[Home Page]|