I have written some code, which will ask for confirmation before leaving the page. and if i say Yes to leave the page, then some cleanup needs to be done. Now the problem is onunload method is getting executed for page refresh but not for page close, why?
Code:
function checkBrowser(){
window.onbeforeunload=warning;
window.onunload = unloadPage;
}
function unloadPage() {
if($('#loginOrNot').val() == 'loggedIn'){
setTheExpertStatusToOffline();
cleanUpChat();
}
}
function warning(){
if($('#loginOrNot').val() == 'loggedIn'){
return "You are leaving the page";
}
}
onunloadevent works only when page is being redirected or refreshed. onlyonbeforeunloadworks while closing..Edit:
Reason in simple terms may be that once browser closes,it doesn’t execute the code that was supposed to happen for
onunloadevent