When the user closes the last tab of our application then we need to logout the user from the system.
When we search about this most of the blogs and StackOverflow answers suggest's to use beforeunload or unload EventListener to detect tab close, but the real problem is beforeunload/unload event listener triggers when the browser reloads, the user navigates to another and tab close
We found this workaround in a StackOverflow, We are little concerned to use the below code. Is there any other way to find the browser tab close.
var validNavigation = false;
function wireUpEvents() {
window.addEventListener('beforeunload', function (){
if (!validNavigation) {
logoutUser()
}
})
$("a").bind("click", function() {
validNavigation = true;
});
$("form").bind("submit", function() {
validNavigation = true;
});
}
$(document).ready(function() {
wireUpEvents();
});