1

To keep things very simple - I have the following code

$(window).bind("beforeunload",function(event) {
    return '';
})

At least on Chrome,FireFox and IE9 - when a user closes the browser - he is presented with two options - "Stay on Page" or "Leave this Page" (The message varies from browser to browser)

Now how can I find out what option the user selected? For sake of simplicity - lets say - I want to show an extra message based on which option the user selected.

Something like - just to give an idea:

if(option=="Stay on Page")
   alert("Thanks for continuing to stay");
if(option=="Leave this page")
   alert("Sorry you decided to go - please come back soon");

How do I capture - what the user selected.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Gublooo
  • 2,550
  • 8
  • 54
  • 91
  • Did you got to know how to detect which option user has selected? "Stay on Page" or "Leave this Page"? If so please share the code sample, I am looking for similar kind of behavior but no sample found till now. Thanks. – emphywork Feb 27 '15 at 12:27

1 Answers1

0

As far as I know, you are not allowed to do that, for security reasons. The only thing you can do is the below:

window.onbeforeunload = function(){
    return 'Your confirmation message';
}
Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
  • 1
    Thanks Mark - my idea is not to show alert messages to the user - all I want to know is if he is really leaving or deciding to stay based on which I have to call a backend task... – Gublooo Jun 30 '12 at 06:22