Skip to content Skip to sidebar Skip to footer

Is There A Way To Determine That The Browser Window Was Closed?

How is it possible to identify that the browser's close button was clicked?

Solution 1:

From the JavaScript Event Reference, the closest match appears to be the OnUnload event. However, this also catches navigation away from the page (and thus you don't want the functions to run if the user actually clicks on a link.)

There is no guarantee that the server will receive the message that the browser window is closed. For example, the older method that involved creating a small pop-up window would be blocked by most modern browsers. Using AJAX might work, but a browser window may close before it attempts to connect.

Solution 2:

would this :

$(window).unload( function () { alert("Bye now!"); } );

help you ?

It is in jQuery though.

you should check the link given by Demoli though, it has further information about this particular method ( and its downside ).

edit : added link.

Solution 3:

Browsers do not have a close event so you can't really tell when the user has closed their browser, there are some potential workarounds in This thread

If you let us know what you are trying to achieve, there may be an alternative way

Solution 4:

This is a hack but it seems to work for me. It might be IE specific as well. I code for an IE only environment.

<bodyonunload="if(self.screenTop>9000){unloadFunction();}">

Edited to add that my unloadFunction opens a popup window that kills the user's session. You can't do anything on the page that was closed itself. 'cause it's closed already! ;)

Solution 5:

You should use onbeforeunload, not onunload. IE 7 (and perhaps other browsers) are not 100% reliable when it comes to firing the unload event, but it seems to always fire the beforeunload event.

for example:

window.onbeforeunload=function(){}

or, if you are using prototype.js:

Event.observe(window, 'beforeunload', function(){});

Post a Comment for "Is There A Way To Determine That The Browser Window Was Closed?"