Share. onbeforeunload event trigger when tries to close, refresh the tab or closing the Browser. Especially on mobile, the beforeunload event is not reliably fired. After that, the handlers for document.body get a turn. It then notifies you, thus taking an "action" on the event. No :) because the solution here is for browser tab close not for the whole browser close. Can we give a sense or put in context the fact that gravity isn't a force yet is one of the fundamental ones? We can use these events to call any javascript handler function. What is the JavaScript version of sleep()? onclick. An alternative is catch the id triggering the event and if it is form dont trigger any function and if it is not the id of the form then do what you want to do when the page closes. When the browser is closed, you aren't really going to be able to capture any server-side code (since actually closing the browser is a client-side operation) however there is a Javascript event that is called prior to a window closing called onbeforeunload which can fire a Javascript function when the browser is about to be closed : Use the cancelable property to find out if an event is cancelable. If the user closes the browser without clicking the logout button, the browser close event detection would require for updating the flag in the database. The idea behind calling a server side code when the browser is closed by the user is based on the new concept introduced by ASP.NET 2.0 AJAX 1.0 Extensions, which is Page Methods. Browser events. Is there any way to execute the script only when window is closed and not when page is refreshed (or) navigated to another page. In a browser, events are handled similarly. onmousedown. Event handlers can be used to handle and verify user input, user actions, and browser actions: Things that should be done every time a page loads Things that should be done when the page is closed Get our Articles via Email. Then browser scrolls immediately providing a maximally fluent experience, and the event is handled by the way. JavaScript's interaction with HTML is handled through events that occur when the user or the browser manipulates a page. This event object contains details of the event. onbeforeunload expects a return string which would be be displayed to the user in a dialog popup. When the browser is closed, you aren't really going to be able to capture any server-side code (since actually closing the browser is a client-side operation) however there is a Javascript event that is called prior to a window closing called onbeforeunload which can fire a Javascript function when the browser is about to be closed : That is, if you hit the Back button on the browser, you will not go to a previous page in … When user closing tab/browser, if you implement a confirmation alert then it will help to solve the both the above-described situation. It then notifies you, thus taking an "action" on the event. onbeforeunload is an javascript event which occurs on window close but there is a problem with it. Asking for help, clarification, or responding to other answers. Really the only way to know if the user still has the browser window open is to set an interval and ping the server ever so often, when the pinging stops do what ever it is you need to do on the server side. For example, you can add the following code in a component: What would realistically be the secret base for someone who can teleport? If you do not need to do something on the server when the client closes the browser, then there really is no real reason to be notified. More importantly I want to point out the difference between the two functions. To learn more, see our tips on writing great answers. Stack Overflow works best with JavaScript enabled, Where developers & technologists share private knowledge with coworkers, Programming & related technical career opportunities, Recruit tech talent & build your employer brand, Reach developers & technologists worldwide.