views:

122

answers:

3

I am trying to have a confirm box come up when the viewer tries to close the window, that allows the user to stay on the current page (by click Cancel), or continue closing the window (by click OK).

My code is as follows...

<script>     
    function confirm_exit(){
        var message = window.confirm("My message.");
        if (message == true) {
            // Output when OK is clicked
            window.close();
        } else {
            // Output when CANCEL is clicked
            ???????
        }
    }
</script>

I am not sure because either one I click closes the window. I need Cancel to remain at the current page, and OK to close the window or proceed with the users window.event.

Hope this makes sense.

A: 

The day you can prevent me from closing my browser window with javascript is the day I ditch that browser as soon as possible for another one in which you can't. It's a security / user issue: it you can prevent closing with javascript, what's to stop you from not even having the politeness of asking to confirm? You should not have this possibility, and browser manufacturers will do everything they can to prevent you from having it.

Wrikken
You should have this ability, and you do...the reason being there are dynamic pages where users edit content / profiles / etc..and when they click the close button you dont think it would be polite to ask them to confirm before they lose all there information? Think about it.
M.Woodard
StackOverflow does this, as well as many other sites I use. I don't see how this is so dangerous.
Dan Herbert
There's a big difference between asking if you're sure you want to leave and actively preventing it.
Charles
"StackOverflow does this"=> not in my browser it doesn't. @M.Woodard: you might be full of good intentions, however, other people may not be. @Charles: aha, from the other answer I gather some browsers have implemented a mechanism for it other then straight javascript confirm, which indeed removes the sting from being actually able to prevent it. True to my word, I happen not to use one of those.
Wrikken
Opera doesn't support this functionality, but not because of any usability issues. The reason is mainly because of a bug in Opera's handling of unload-type events. Opera does plan on implementing this functionality at some point. If you do some searching on their dev documentation and forums they make mention of it.
Dan Herbert
A: 

Courtesy of a certain website with a dash in it's name:

window.onbeforeunload = leaveConfirm;
function leaveConfirm() {
    var leaveThePageMessage = 'Are you sure you want to leave this page?';
    return leaveThePageMessage;
}​

This should work in all browsers (except for Opera).

Dan Herbert
"This should work in all browsers" => no success for Opera.
Wrikken
@Wrikken Fortunately, Opera accounts for a very small portion of most sites' market share. Less than 1% usually, if even that much, so that should not be a major problem. In the case of Opera users, I think JS continues to run after you navigate away so you may actually be able to safely save what a user may have done on a page so they don't lose any work in Opera.
Dan Herbert
+1  A: 

Try:

window.onbeforeunload = function()
{
  return confirm('Are you sure you want to close?');
}
Kranu