Reputation: 8639
how can I catch that the browser has gone to exit now using javascript.
Upvotes: 0
Views: 228
Reputation: 3614
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');
};
Upvotes: 0
Reputation: 1980
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.
Upvotes: 1
Reputation:
It only works with back, reload and tab close, not with complete browser close.
<html>
<body onunload="alert('Thank you for visiting');">
<body>
</html>
Upvotes: 0
Reputation: 3196
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?
Upvotes: 2