IE OnBeforeUnload Fun

Was debugging something today when I made a discovery. Using Internet Explorer’s onbeforeunload event handler, you can automatically pop up a confirmation window asking the user to continue navigating away or stay on the page with ever using confirm(). All you have to do is set your message in the window.event.returnValue property, and the browser handles it automatically. For instance:

window.onbeforeunload = function () {
    window.event.returnValue = "All your work will be lost.";
}

This results in a window being popped up that shows this text as well as, “Are you sure you want to navigate away from this page?” before it and “Press OK to continue or Cancel to stay on the current page.” Pretty cool undocumented feature.

Understanding JavaScript Promises E-book Cover

Demystify JavaScript promises with the e-book that explains not just concepts, but also real-world uses of promises.

Download the Free E-book!

The community edition of Understanding JavaScript Promises is a free download that arrives in minutes.