0

how can I catch that the browser has gone to exit now using javascript.

jmj
  • 237,923
  • 42
  • 401
  • 438
Bajrang
  • 8,361
  • 4
  • 27
  • 40
  • 2
    What do you mean by "gone to exit" exactly? Closing the browser? Closing the current page? – Pekka Feb 16 '11 at 09:21

4 Answers4

2

If you are looking for catching the window close event try something like this:

jQuery(window).bind('beforeunload', function() {
    return confirm("Do you really want to do that?");
});

found here: How to capture the browser window close event?

Community
  • 1
  • 1
jon3laze
  • 3,188
  • 6
  • 36
  • 69
1

If the browser has gone to exit (exited) there is no javascript running anymore, so the answer would be no.

But I'm guessing you are looking for the window.onbeforeunload event.

the JinX
  • 1,950
  • 1
  • 18
  • 23
0

It only works with back, reload and tab close, not with complete browser close.

<html>
<body onunload="alert('Thank you for visiting');">

<body>
</html>
0

For JQuery: (source: http://api.jquery.com/unload/)

$(window).unload(function() {
  alert('Handler for .unload() called.');
});

And if you want native Javascript, this works on my Firefox3.6 and Chrome. I do not have any other browsers to test it on right now, but after some google-ing, it looks like it could work on IE too, but Opera might have some issues(?). You should test it for browser compatibility first.

window.onunload = function () {
    alert('unloading');
};
aiham
  • 3,614
  • 28
  • 32