Is there a way in Javascript to check ONLY for user Closing a tab/window of the browser?
window.onbeforeunload
is very generic because it ALSO gets triggered when user hits F5 key for refresh, or when user clicks any <a href
link or form submit
.
Similarly, document.addEventListener('visibilitychange',
is also very generic & it too gets triggered for all the above.
Checking for keyCode(116) like following code is specific to checking only when user hits F5 key:
function fkey(e) {
if (e.keyCode == 116) {
e.preventDefault();
// do stuff...
}
}
I am looking for similar (keyCode or any other alternatives), that can capture ONLY when user Closes a tab/window of the browser, not including other events like Refresh or link clicks.
PS: SO does not seem to have an answer for this. If any exist, let me know through comments & I’ll close this Q.