How do I ask for confirmation when closing a window?

I want to display a confirmation message if the user wants to leave the current page, just like stackoverflow does it when you enter a question.

Can anyone help me with this by providing me with a script or redirecting me to an answer (on this site or elsewhere)?

thank

+2


source to share


5 answers


You need to process the window onbeforeunload

and return a confirmation message as a string.



onunload

won't work here. Demo

+2


source


You can use event onbeforeunload

.



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

      

+8


source


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.

+1


source


You will need to store all the values ​​of your fields in some variables and check them whenever the page url changes or any link is clicked to catch the page unload event using the page.

Event unloading page / sample http://help.dottoro.com/ljflhicd.php .

0


source


Doesn't this work?

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

      

Also for framesets by the way ...

-1


source







All Articles