View Full Version : How can I pop up a confirmation box on browser exit?

10-19-2005, 11:18 PM
Hi everyone,
I've been stuck on this problem for a little while now.

Basically, I have an ASP.NET page that's holding a lot of data. In case a user accidentally clicks the exit or back button, I want to pop up a javascript confirmation box that says 'Are you sure you want to exit?' and if the user clicks 'ok', the browser exits, and if the user hits 'cancel' it returns to the page, like the user never clicked the [x].

I can pop up a confirmation easily enough, but what methods do i need to use to keep the user or to close the browser? Also, I would like it if it only popped up on closing the browser or hitting back, not on a refresh, as the ASP page does a lot of post-backs.


10-19-2005, 11:26 PM
You could use a confirm box and a redirect back to the current page to handle anyone trying to leave the page but with the browser still open.

If they close the browser window then the browser is closed and there is nothing you can run to stop it.

10-19-2005, 11:31 PM
Hmmm, I thought that if I caught it with onunload() then I could do something before it exited. From what you said though, it sounds like I can do anything besides keep the browser open. Is that right?

If so, then I'll have to somehow pull the data off the asp page and maybe serialize it and put it in a cookie. Then it should be restoreable the next time the person visits the page. I can pull it back out of the cookies and repopulate the ASP page. Does that sound like it will work?

One other idea...could I use onunload to hit the submit button automatically when the browser exits?