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.
The addEventListener() method is used to set up a function whenever a certain event occurs. The beforeunload event is fired just before the windows and its resources are to be unloaded. When this event is fired, the webpage is visible and the event is still cancellable at this point.
The event handler should call preventDefault() to show the confirmation dialog according to the specifications. If the user wishes to continue to a new page, then it navigates to the new page, otherwise the navigation is cancelled. However, older browsers may not support this method and a legacy method is used in which the event handler must return a string. This returned string can be empty.
Some browsers may not decide to show any confirmation boxes unless the user has interacted with the page. This is used to prevent unwanted or malicious websites from creating unnecessary pop-ups. The user may have to interact with the page to see the confirmation message.
This method works for detecting both when a tab is being closed or when the browser is being closed.
Syntax:
window.addEventListener('beforeunload', function (e) {
e.preventDefault();
e.returnValue = '';
});
Example:
<!DOCTYPE html> <html> <head> <title> How to detect browser or tab closing </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b>Detect browser or tab closing</b> <p> The beforeunload function is triggered just before the browser or tab is to be closed or the page is to be reloaded. Modern browsers require some interaction with the page, otherwise the dialog box is not shown. </p> <form> <textarea placeholder = "Trigger an interaction by writing something here"> </textarea> </form> <script type="text/javascript"> window.addEventListener('beforeunload', function (e) { e.preventDefault(); e.returnValue = ''; }); </script> </body> </html> |
Output:
- Before navigating the tab:
- After trying to navigate away from the page:
- After trying to reload the page:
- After trying to close the browser:
Recommended Posts:
- How to remove active nav-tab when click outside of nav-tab in Bootstrap ?
- How to close current tab in a browser window using JavaScript?
- How to detect the user browser ( Safari, Chrome, IE, Firefox and Opera ) using JavaScript ?
- How to detect the browser language preference using JavaScript ?
- How to detect HTML 5 is supported or not in the browser ?
- How to detect Browser Language in PHP?
- How to detect the version of a browser ?
- How to Open URL in New Tab using JavaScript ?
- How to use modal closing event in Twitter Bootstrap ?
- How to handle the modal closing event in Twitter Bootstrap?
- How to automatically close all collapsible elements inside of the accordion when closing the accordion?
- How to refresh parent page on closing a popup ?
- CSS | tab-size Property
- How to tab space instead of multiple non-breaking spaces (nbsp)?
- How to Create a Tab Image Gallery ?
- How to open a hyperlink in another window or tab in HTML ?
- Semantic-UI | Tab
- How to make bootstrap version 2 tab dropdown?
- How to set active tab style with AngularJS?
- How to force tab-navigation to stay in place using Bootstrap ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


