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.
Page___History____Comments
________stack
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.
Page____History____Comments
_________stack
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.
Page____History____Comments
_________stack
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!
A
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:
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.
Enjoy!
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.
Page___History____Comments
________stack
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.
Page____History____Comments
_________stack
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.
Page____History____Comments
_________stack
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!
A
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:
| Code: |
|
<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.
Enjoy!
