-5

I know that by using this function in java script:

window.onbeforeunload = function(e) {
return 'Please press the Logout button to logout.';
};

It will show the warning message when I press the close window button. But the warning message will ask user to choose between 'stay on the page' or 'leave the page'.

I don't want the user to choose when close window. How do we create the warning message with just OK button?

Coolguy
  • 2,225
  • 12
  • 54
  • 81
  • 1
    A [warning before closing the window](http://stackoverflow.com/questions/15514674/show-warning-message-before-close-window-in-java-script) is fine, but you can't block the user from exiting, nor would such a method be reliable. – Aaron Blenkush Mar 20 '13 at 04:23

2 Answers2

2

That would have serious implications for sites that wanted to use this for less-than-honorable intentions (pop-up ads that had no choice but to click through or keep open?). It sounds to me like you just want to ensure users don't "slam the page shut" without logging out, which isn't anything malicious, but it's definitely unnecessary.

There are a number of different ways to go about authentication session management, but you have to keep this in mind:

There's no way to guarantee the user does anything you want them to, so you need to handle those cases on your end.

Aaron Blenkush
  • 3,034
  • 2
  • 28
  • 54
0

You can't. That would essentially allow the developer to prevent the user from leaving the page. Browsers protect the end user from that behavior.

You can never rely on the onbeforeunload event anyway. What would happen to your code if the user simply closes the browser? Or the computer crashes. You need to handle all these cases in your logic, without relying on some browser event.

Steve
  • 8,609
  • 6
  • 40
  • 54