How to show confirm dialog before user closing browser tab or window

Posted by & filed under ASP.Net, Javascript, Miscellaneous.

Recently in ASP.Net forums, I found a question like how to show an alert box before user closing or leaving browser window or tab. You can find the similar dialog, in StackOverflow.com , when you are started answering a question and then trying to close window / tab or navigating from it without submitting the answer. And here is the snippet which will help you to do this.

function closeIt()
{
  return "Any string value here forces a dialog box to \n" +
         "appear before closing the window.";
}
window.onbeforeunload = closeIt;

This will display a dialog like this

Navigation confirmation dialog

Navigation confirmation dialog

Note: This confirm dialog will also appear when you start navigating from the page to another also.

No related content found.

One Response to “How to show confirm dialog before user closing browser tab or window”

Leave a Reply

CAPTCHA Image
*