收录日期:2019/12/07 12:50:27 时间:2010-10-28 14:12:50 标签:javascript,browser,gmail

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?

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.

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.