Does anyone know any way that I can use javascript to check when the browser window is close and pop-up a confirmation dailog to ask whether the user is confirm to exit the browser or change his mind to stay?
+5
A:
window.onbeforeunload = function (e) {
var e = e || window.event;
//IE & Firefox
if (e) {
e.returnValue = 'Are you sure?';
}
// For Safari
return 'Are you sure?';
};
Chad Grant
2009-04-30 05:39:41
That should probably be 'return confirm("Are you sure? ...");'.
Anonymous
2009-04-30 05:41:10
@Anon nope, confirm() returns a boolean, your handler for onbeforeunload is required to return a string.
Chad Grant
2009-04-30 05:47:20
Well, that's a peculiar behavior. Goes to show how often I've used it!
Anonymous
2009-04-30 05:50:08
+1
A:
If the browser remains running after the page is closed, and if the browser processes the "onbeforeunload" event of the body element (sometimes it's disabled), and if the browser allows popup windows or mesage boxes and the ability to return false from that event to prevent the page change, then it's possible.
For an example, start typing a comment on any stackoverflow page with Javascript enabled and then navigate away from that page.
Anonymous
2009-04-30 05:39:54