0

Is there any way that I can identify that the user clicked the "Back" button before this page? This is the none-existed example I have in my head:

window.onload = new function(e){
  if(e.type == "back"){
   //do some operation
  }
}

Is there a way to know that? Thanks

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
SexyMF
  • 10,657
  • 33
  • 102
  • 206
  • possible duplicate of [detect back button click in browser](http://stackoverflow.com/questions/6359327/detect-back-button-click-in-browser) – Darin Dimitrov Jan 29 '12 at 15:29
  • What are you trying to do? Something tells me there's a better way then trying to catch a back button. Also, I don't believe this is possible, and [`onbeforeunload`](https://developer.mozilla.org/en/DOM/window.onbeforeunload) is really only dependable in IE. – Brad Christie Jan 29 '12 at 15:30
  • short answer: **you cant**, in a easy or cross-browser way. Also read: [similar #1](http://stackoverflow.com/questions/2498672/detect-if-the-user-has-used-the-back-button) and [similar #2](http://stackoverflow.com/questions/55871/track-when-user-hits-back-button-on-the-browser) – c69 Jan 29 '12 at 15:33
  • I think the sentence "http is stateless" means something: in this case, you can't watch a *previous state* of a webpage (location.hash for example)., I agree with c69, this is not possible – Francesco Belladonna Jan 29 '12 at 15:49

2 Answers2

0

Within your own site, and with a lot of work, you could do it with a cookie, but you would have to hook every link.

Mark Robbins
  • 2,427
  • 3
  • 24
  • 33
-2

yes, easily.

window.onbeforeunload = askConfirm;

function askConfirm(){

return "You have unsaved changes.";

}
PaperCut
  • 1
  • 1
  • 2