Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture event onclose browser

Tags:

How I can capture event, close browser window, in jQuery or javascript ?

like image 918
AlexC Avatar asked Jun 22 '09 07:06

AlexC


People also ask

How do you handle a closed event browser?

To handle the browser tab close even in React: Use the useEffect hook to add an event listener. Listen for the beforeunload event. The beforeunload event is triggered when the tab is about to be unloaded.

How can I tell if my browser window is closed?

A tab or window closing in a browser can be detected by using the beforeunload event. This can be used to alert the user in case some data is unsaved on the page, or the user has mistakenly navigated away from the current page by closing the tab or the browser.

How do you handle browser tab close angular not close refresh?

The best way I found to reload the page without losing cookies, and remove cookies on window or tab close was to use ng-keydown to watch the F5 Keypress (KeyCode 116).

How do I close a JavaScript browser?

JavaScript provides an in-built function named close() to close the browser window that is opened by using window. open() method.


2 Answers

http://docs.jquery.com/Events/unload#fn

jQuery:

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

or javascript:

window.onunload = function(){alert("Bye now!");} 
like image 188
jeroen.verhoest Avatar answered Sep 30 '22 05:09

jeroen.verhoest


You're looking for the onclose event.

see: https://developer.mozilla.org/en/DOM/window.onclose

note that not all browsers support this (for example firefox 2)

like image 39
Jonathan Fingland Avatar answered Sep 30 '22 04:09

Jonathan Fingland