Is there any way to call a JavaScript "onbeforeunload" function only when the user clicks the browser close button?
Currently, it triggers the event on page refresh, page navigation and browser exit.
Is there any way to call a JavaScript "onbeforeunload" function only when the user clicks the browser close button?
Currently, it triggers the event on page refresh, page navigation and browser exit.
There is not a close action for the browser in Javascript. The closest you could get to this implementation would be Javascript's close() function. However, you would need to rely on your user to click this button to close the window each time for this to work properly. Generally speaking, it is not feasible.
Here is some more information about Javascript's close() function.