Skip to content Skip to sidebar Skip to footer

How To Ask Confirmation When Closing A Window?

I want to pop up a message asking for confirmation if the user wants to leave the current page, exactly like stackoverflow does it when you are typing a question. Could someone hel

Solution 1:

You could use the onbeforeunload event.

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

Solution 2:

You need to handle the window's onbeforeunload event and return a confirmation message as a string.

onunload will not work here. Demo


Solution 3:

window.onbeforeunload Seems to work for IE, but something like

window.onunload = function()
{
    if (confirm('Save changes?'))
        document.forms['form1'].submit();
};

Seems to work for all browsers.


Solution 4:

You will have to store all the values of your fields in some variables & check them whenever use changes the page url or clicks on some link, to trap the page change event use page unload event.

Page unload event link/ sample http://help.dottoro.com/ljflhicd.php.


Solution 5:

Doesn't this work?

<body onunload="return confirm('really quit?');">

Also for framesets by the way...


Post a Comment for "How To Ask Confirmation When Closing A Window?"