Topic awaiting preservation: Back Button Detection (Page 1 of 1) |
|
---|---|
Nervous Wreck (II) Inmate From: Dublin, Ireland |
posted 10-27-2003 13:14
Can anyone tell me if it is possible to detect when a page is being displayed because the user pressed the back button rather than by following a link? |
Nervous Wreck (II) Inmate From: Indianapolis, In USA |
posted 10-27-2003 13:48
An age-old problem. As far as I know, it's practically not possible. |
Paranoid (IV) Inmate From: France |
posted 10-27-2003 14:18
You can check the window.history object. If window.history.current != window.history[ window.history.length -1 ], the chances are big that the users pressed the back button. But I'm not quite sure how to handle the users going back and forth in the history, nor how to handle if someone goes several times to the same page during the same session. |
Maniac (V) Inmate From: Brisbane, Australia |
posted 10-27-2003 17:08
Interesting, is there any particular reason why you'd want to do this? |
Lunatic (VI) Mad Scientist From: Massachusetts, USA |
posted 10-27-2003 17:10
The general answer to "I want to control the user's back button or history" is "find another way to accomplish what you're trying to do." Pretty much anything you try to do to manipulate the back button or the history won't work right in all cases. |
Nervous Wreck (II) Inmate From: Dublin, Ireland |
posted 10-27-2003 19:46
Thanks to poi, I'll try that solution tomorrow. In case there's another way, let me explain what I want to do. I have a button on a web page which when pressed displays a page called random.htm. This contains a bit of Javascript which then redirects the browser to a random web page within my site - this works perfectly. The problem occurs when the user presses the back button. The previous page in the history is random.htm, so what happens is that the user gets redirected to another random page. What I want is to be able to detect this situation so I can redirect the browser to the initial page with the random button on it - i.e. to behave as though the random.htm page was never displayed. |
Paranoid (IV) Inmate From: France |
posted 10-27-2003 20:09
Simon Figg: If you don't want to see the random.htm page parasiting your history, use the location.replace( ... ) function to redirect to the page selected at random in your site. This function crush current entry in the history. And voilà. |
Nervous Wreck (II) Inmate From: Dublin, Ireland |
posted 10-28-2003 10:11
Poi, that's perfect. Many Thanks! |