Is there a function in JS that detects only the last tab/browser close? Or is there a ways that when the last tab/browser is closed, logout function will trigger?
I tried the one below but it did not work.
var warnBeforeClose = true;
function unloadPage() {
if (performance.navigation.type == performance.navigation.TYPE_RELOAD) {
dontWarn();
}
if (warnBeforeClose) {
$(".logoutButton").click();
return "Your changes will not be saved.";
}
}
window.onbeforeunload = unloadPage;
// ...when the elements exist:
$("a").click(dontWarn);
$("button").click(dontWarn);
$("form").submit(dontWarn);
$("input[type=submit]").click(dontWarn);
function dontWarn() {
// Don't warn
warnBeforeClose = false;
// ...but if we're still on the page a second later, set the flag again
setTimeout(function () {
warnBeforeClose = true;
}, 1000);
}
1