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) |
| [start] | A |
| link to B | AB |
| link to C | ABC |
| back button | ABC |
| forward button | 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.
As it happens, there is a means by which an author can simulate a back button. It involves JavaScript and is, therefore, not going to work non-JavaScript browsers or on those browsers whose users have disabled JavaScript.
The trick to using this method is to know how to do it right. Unfortunately, the most common variation of this means to be passed around is not the right way to do it and is, therefore, useless to non-JavaScript browsers. Here is the popular, wrong way to do it:
<A HREF="javascript:history.back()">Go back</A>
The above method is unsafe because the location that it points to, javascript:history.back(), is an invalid URL. The safer means makes use a valid URL and is therefore safe for non-JavaScript aware browsers. The safe means follows:
<A HREF="previous.html" onClick="history.back();return false;">Go back</A>
The above method makes use of both the JavaScript solution and the "best guess" method. JavaScript aware browsers will move back in the browser's history list due to the use of the history.back() method and non-JavaScript aware browsers will activate the link to previous.html which, of course, should be the URL for your best guess at what the previous page was.
| [Section Index] | [Home Page] |