Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?


2 Answers

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.

like image 95
Raymond Martineau Avatar answered Dec 31 '25 18:12

Raymond Martineau


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.

like image 37
andyk Avatar answered Dec 31 '25 18:12

andyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!