18

I would like to know if there is any way to handle the browser exit event.

For example, I would like to send a query when the user click on the cross-exit or simply close his browser.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
stormblow
  • 388
  • 1
  • 2
  • 12

2 Answers2

16

I think I have found a solution. I haven't tested it on IE but it seems to be working on Firefox :

Ext.EventManager.on(window, 'beforeunload', function() {
    alert('cross-exit tab click AND cross-exit browser click');
});

Ext.EventManager.on(window, 'unload', function() {
    alert('cross-exit tab click');
});

PS : Thank you exhuma for having edited the question ;)

stormblow
  • 388
  • 1
  • 2
  • 12
  • FYI: this works only on browser refresh. Doesn't work when the page "unloads" because of browser (Chrome) Back or Forward buttons. Still good to know, thanks. – Joseph Victor Zammit Jul 24 '12 at 20:34
7

This thread might shed some insight: http://forums.devarticles.com/javascript-development-22/how-to-stop-browser-from-closing-using-javascript-8458.html

I haven't given this solution a test-run yet. But it seems possible.

Here's a more in-depth discussion on the onbeforeunload event handler. http://bytes.com/topic/javascript/insights/825556-using-onbeforeunload-javascript-event

And then there's always quirksmode. Unfortunately the load/unload events are not yet covered by quirksmode. As stated on the page:

I haven’t tested the load and unload events yet; they’ll be added to the table later on.

exhuma
  • 20,071
  • 12
  • 90
  • 123