How to detect browser or tab closing in JavaScript ? Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Detecting browser or tab closure in JavaScript is essential for preventing data loss or unintended navigation. Using the beforeunload event, developers can prompt users with a confirmation dialog, ensuring they don't accidentally leave a page with unsaved changes or important information. The beforeunload event is triggered just before the window is unloaded. To handle this event, you can use the addEventListener() method to attach a function that shows a confirmation dialog. This dialog can alert the user and prevent the page from closing.However, some modern browsers may only show this dialog if the user has interacted with the page to avoid unnecessary pop-ups from malicious sites.Syntax:window.addEventListener('beforeunload', function (e) { e.preventDefault(); e.returnValue = '';});Example: In this example, we detect browser or tab closing in JavaScript.The beforeunload event triggers before closing or reloading the browser/tab.A prompt is shown to confirm the action when e.returnValue is set.Modern browsers require user interaction, like typing in a textarea, for the dialog to appear.The script prevents the default action to enable the confirmation dialog. html <!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:JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples. How to detect browser or tab closing in JavaScript ? Comment More infoAdvertise with us Next Article How To Change The Browser To Fullscreen with JavaScript? S sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies Web technologies javaScript JavaScript-Questions +1 More Similar Reads How to close current tab in a browser window using JavaScript? In this article, we will see how to close the current tab in a browser window using JavaScript. window.close() methodTo make this, we will use window.close() method.  This method is used to close the window which is opened by the window.open() method. Syntax:window.close();But accounting for a secur 1 min read How to return true if browser tab page is focused in JavaScript ? There may be situations when we need to check if a browser tab is focused or not. The reasons for this include: Prevent sending network requests if the page is not being used by the user as this would reduce the amount of load on the server. Also, this would save upon the cost if we are using a paid 4 min read How to Detect Idle Time in JavaScript ? The idle time is the time that the user doesn't interact with a web page. This interaction can be either moving the mouse, clicking on the page, or using the keyboard. This time can be detected to execute certain events that may need to occur after a certain period of idle time. \ Method 1: Using Ja 4 min read How To Change The Browser To Fullscreen with JavaScript? In web development, there may be scenarios where you want to display your web application or a specific element in full-screen mode, covering the entire browser window. This can be useful for creating immersive experiences, such as games, presentations, or video players. JavaScript provides a built- 2 min read How to create a PHP detection browser script ? A browser script is used to find the complete information of the web browser. A web browser is working on the hypertext transfer protocol and retrieves all the information on the internet and displays it on your desktop so that you can access all the things from anywhere. In this article, we will s 2 min read How to detect browser autofill using JavaScript? We will learn how to detect browser autofill using javascript. Here we are going to use 3 programming languages HTML, CSS, Jquery to solve this problem in the best and well-defined way. In all web browser autofill is the feature that automatically fills the form fields such as email, address, passwo 4 min read Like