Technology

JavaScript: Wait! Are you really sure you want to leave?

StopHave you ever gotten all the way to a submission page only to mess up and close the browser window? I have! I’ve actually gone through an entire purchasing system and added tons of items only to accidentally close the browser. Since most browsers support tabs now, it’s quite easy to do.

Applications that have a multi-step process, review and checkout would be wise to utilize a confirmation prior to the window closing… but it’s not as easy as you think. JavaScript has an onbeforeunload event where you can confirm whether or not the user really wants to leave the page. Here’s an example and here’s the code:

One comment

  1. 1

    Yip, I’ve also experienced that crazy little problem on browser window. Actually, I’ve searched and found the solution to that problem and I think it is little bit different from your code. Only your code looks great and and simplified it. Thanks

This site uses Akismet to reduce spam. Learn how your comment data is processed.