onbeforeunload

The onbeforeunload event in JavaScript will allow you to interrupt someone leaving a web page, often used as an alert if there is unsaved form data that will be lost, or if an Ajax call has not yet completed.

Some browsers (such as Chrome) will allow you to customize the text in the dialog box, while others (such as Firefox) ignore any custom messages and display a boilerplate message: “This page is asking you to confirm that you want to leave – data you have entered may not be saved.” This is an interesting decision because it discourages the annoying practice of spamming a user with messages as they leave your site, while maintaining the one legitimate use case of such a dialog box.