views:

36

answers:

2

How Gmail shows this alert "Exit this page?

Your draft has Been modified. Your draft has Been modified.

Click OK to continue or Cancel to stay on the current page." when you are writing a email and you try to close the browser tab?

+2  A: 

That's the onbeforeunload event (MSDN docs, Mozilla docs). They've added a handler to fire that prompt when you try to unload the page.

In particular, if you register a function that returns a string as the event handler, the user agent will pop up an "OK/Cancel" dialog box containing the string as a message.

Andrzej Doyle
Thanks. But I want to add the condition, if the user do not click on the browser close button but simply refresh the page, in this case the onbeforeunload function must not be executed. How can I do that?
albertopriore
A: 

This feature relies upon the onbeforeunload event that most browser provide. Note that different browsers give you different options for what kind of text and customizable button titles you can have. I believe Chrome gives you the most options, here.

pkananen
Thanks. But I want to add the condition, if the user do not click on the browser close button but simply refresh the page, in this case the onbeforeunload function must not be executed. How can I do that?
albertopriore