You are invited to Log in or Register a free Frihost Account!

Simulating the back button

Simulating the back button

Often people ask if it is possible to simulate the back button of browsers by adding some kind of link to their page. The answer is that it is not possible using HTML nor CGI, but to understand that, we first have to know how the back button usually works.

Although we will speak of the back button in this document, the same argument holds if a browser is used where the back function is obtained by other means, for instance a keystroke or simply saying the word back.
How the back button works

Usually, the back button works as follows:
The browser uses a stack to remember visited pages. Each time a link is followed, or the user gives an URL to retrieve, the browser will push the current URL on its stack. If the user selects the back function of his browser, the browser will go to the document whose URL is saved on the top of the stack (if the stack is non empty) and will pop the URL from the stack (1).

Suppose a user starts his browser and visits pages A, B and C in that order. Let us see how the history stacks behaves.
Figure 1: Visiting pages A, B and C.


A_________________A is the first page visited. There are no previous visited pages, so the history stack is empty.

B_______A_________ Visiting the second page B will cause A to be put on the history stack.

C_______B A________Now the third page is visited and B is pushed on the history stack. The stack will contain two pages now.
Adding a link back

It is not hard to add a link back to the previous visited document. Often browsers send the URL of the previous visited document to the server in the HTTP header; the server can pass that variable to a program which constructs the page with the link back. (2)

However, that does not mean such a link is the same as a back button! Let us continue the example and assume pages B and C have the back links described in the above paragraph.
Figure 2: Using two back links from page C.


C________B A_______This is the situation as we had before. Now, the user follows the back link, assuming it does the same as the back button.

B________C B A_____The user is now back on page B, and follows the back link found there as well.

C________B C B A___And now is the user back on page C again! This happens because B was accessed from C and hence C is the previous page.

In fact, if the user would continue to follow all the back links he encounters, he will ping pong between pages B and C forever.

For the contrast, suppose the user is at C (after the first example) and uses the back button twice:
Figure 3: Using the back button twice.


C_______B A________This is the situation as we had before. Now, the user uses his back button.

B_______A__________Just as in the previous case, the user is on page B. But note that his history stack is smaller, not larger!


And after using the back button again, the user is back on page A, and his history stack is empty. Neither reaching page A or getting an empty history stack is possible using the back links.

One may argue one can avoid playing ping pong between pages B and C by using more information than just the previous page. When the user first visits B, the server knows the previous page of the user was A, and can put that information in the URL pointing to C. Then one could put some extra information in the URL pointing back as well, so that if B is reached via the back link from C, the back link on B points to A. In theory this is true. It will fail however when the author of C is unaware of the existence of B, or the other way around. And that is a rather common situation on the web.

JavaScript To The Rescue!

According to the documentation of JavaScript [NCC 95e], it gives the possibility of simulating the back button. The following ought to do the trick:

<a href = "javascript:history.back()"> Go back </a>

Note however that this requires a user agent that is both JavaScript aware, and has JavaScript enabled. The construct will fail for all other browsers.

I hope this is readable with all the formatting I had to do to get it on here.

I suppose that you will need a background page to keep track of the pages that you are currently viewing. Only then will you be able to use the back function without javascript.
Isn't it possible to implement a home made stack manager on the client? using javascript for instance, and that uses the HTTP messages to extract URL's and then stores them with regard to a classic push pull stack instructions. I think it is very possible to program such a thing.
Using CGI or SSI to create back and forward buttons would be as simple as savng visited sites in a delimited list passed between cgi generated pages. This list could be parsed into an array and used as a back, back-to, forward or foward-to link. By saving the position of the pointer in the array and incrementing or decrementing the pointer as you navigate; the forward and back should always remain relative until the user 'clicks-out" of your site. You could present a list in a pop-up or select list for the user to skip back or forward as they wished.
Of course, this would only work as long the user was within the confines of your environment. But then, this would also be true of your javascript.
Related topics
best flash template website..?????!?
Auto redirect your visitors to a different page
PHP Validation Class
do you like my site design ?
Need help
The noob needs help/Plz
Here is my art for you
Webpage Display Problem
I need help with CSS
TrendyFlash Site Builder 1.0
Getting page could not be displayed
How to expired a page so a user cannot press back button ?
How can I disable the back button of my browser?
Cancel Button?
Reply to topic    Frihost Forum Index -> Webmaster and Internet -> Design Tips

© 2005-2011 Frihost, forums powered by phpBB.