6

I've no doubt that these are probably documented on the internet somewhere and indexed by Google - but after wading through pages and pages of links to discussions about disabling the functionality I thought I'd ask to see if anyone can give useful answers about merely detecting their usage.

The method described in one of the better articles on disabling the back button only works by creating a page transition - so not much use for detection.

The presence of window.history.next might provide a mechanism for detecting the back button - but how widely supported is it? Does it require unusual privileges?

Jonathan's answer to this post looks promising - but pre-supposes that I can set the window name on the first landing - I guess I could use the absence of a cookie to detect first hit. Any other ideas?

TIA

Community
  • 1
  • 1
symcbean
  • 47,736
  • 6
  • 59
  • 94
  • 2
    Jonathan's answer my look promising, but the accepted answer by bobince is the correct one: You can't. The only reasonable way is to build your application server-side so that it works even when the user goes back. It's not that difficult and protects against other "unexpected" user actions such as reloading, submitting forms twice, opening additional windows, etc. – RoToRa Mar 21 '11 at 14:12
  • I've been playing around and can detect most instances of new windows using history.length in MSIE and Firefox. i.e. Jonathan's answer is valid for most new windows (see my comment there). But regarding the back button: history.next is protected in Firefox. Unfortunately fixing the current codebase is not an option. – symcbean Mar 21 '11 at 16:14

2 Answers2

0

Use sessionStorage/localStorage.

if (!sessionStorage.hasBeenHereBefore) sessionStorage.hasBeenHereBefore = true;
else {
    // Code to run if they've been here before.
}

If you use sessionStorage, then if they close the browser and come back, it will seem like the first time, but if you use localStorage then it'll stay forever.

You might try looking for some secret events like:

window.addEventListener('historyback', function () {}, false);
window.addEventListener('navigateback', function () {}, false);
window.addEventListener('returntopage', function () {}, false);
McKayla
  • 6,879
  • 5
  • 36
  • 48
  • I think that's an answer to a different question - not the one I asked. The problem I'm trying to mitigate is where the state a serverside session does not match the current location selected by a user. – symcbean Mar 21 '11 at 17:46
  • I know, but really this is as close as you're going to get right now. You could try a bunch of window events. (Examples in the answer) – McKayla Mar 22 '11 at 16:45
0

I wrote some code a while back. The script was never fully tested, but you should be able to pull out some techniques to get the same effect as to detect the back button from it.

http://pastebin.no/32fx

Christian Tellnes
  • 2,070
  • 15
  • 17
  • Thanks Christian - I came across this post (http://stackoverflow.com/questions/136937/is-there-a-way-to-catch-the-back-button-event-in-javascript/2348076#2348076) since asking my question which takes a similar approach. This definitely looks like the right method of solving the problem – symcbean Mar 24 '11 at 23:16
  • If you end up using my script, I would like to see what you've done with it:) – Christian Tellnes Mar 25 '11 at 02:10