When the page loads I set: var lock = 0;
I change lock to 1 if I want to provide a warning to the user if they are about to close their page and have pending actions. I sometimes change it to 2 if I need a different warning. If lock is set to 0 then I don't want a warning message to appear as the user closes their page.
This shows my custom warning message just fine every time the user moves away from the page, regardless of lock setting.
window.onbeforeunload = function(){
return 'Are you sure you want to leave this page?';
}
This only shows when lock is 1 or 2 but doesn't show my custom warning message.
window.onbeforeunload = function(){
if(lock == 1) return 'Are you sure you want to leave this page?';
else if(lock == 2) return 'Bad things will happen if you close this page!';
}
Is there a way to have a conditional custom warning message? It seems like if I have anything other than 'return' inside the function then it uses the default message.