0

I'm trying to open a modal window when the browser window closes. Is there a way to do this with window events? Thanks.

  • 2
    What OS? What language? What browser? Seriously? – Nick Veys Jul 31 '09 at 01:27
  • 2
    Not to mention... why? I would find this rather annoying. – Greg Hewgill Jul 31 '09 at 01:31
  • yeah, I know, it's not really user friendly, but it's for an intranet of a client. they want to do a user satisfaction survey (kind of contradictory). the modal window to accept participation should come up at the end of the visit. 90% of users are employees with windows xp and IE7, the rest is various versions of FF and safari on mac OS. –  Jul 31 '09 at 09:29

3 Answers3

2

onbeforeunload is probably your best bet.

MyItchyChin
  • 13,733
  • 1
  • 24
  • 44
  • 1
    onbeforeunload can't distinguish between browser closing, and navigating to another page... – Stobor Jul 31 '09 at 01:42
0

Short answer: no: Most browsers don't let you know when the browser is being closed.

See also:

Java Servlet : How to detect browser closing?

How capture event for browser closing in web applications?

Community
  • 1
  • 1
Stobor
  • 44,246
  • 6
  • 66
  • 69
  • This isn't accurate in my experience. I've been using onbeforeunload for years and almost never run into any issues. – Jon Jul 31 '09 at 01:35
  • @Jon: technically Stobor is right, onbeforeunload fires when a page unloads which happens not just when the browser is closing but also when you're navigating away from the page. – MyItchyChin Jul 31 '09 at 01:39
  • @CptSkippy: That's a good distinction. It's hard to say if the OP was trying to be that specific. – Jon Jul 31 '09 at 02:21
  • Thanks guys! That event will do it. Actually, I need to catch the user closing the browser as well as navigating away. –  Jul 31 '09 at 09:49
0

You can execute whatever you need to with the onunload JavaScript event. I'd probably do a window.open rather than try to create a modal because I think the browser will still close. You could use the onbeforeunload event to show a modal confirming their action if that's what you're hoping to do.

Jon
  • 2,085
  • 2
  • 20
  • 28