By default var lock is set to 0.
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. I sometimes change it to 2 if I want a different warning. If lock is set to 0 then I don't want a warning message to appear if they are about to close 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) return 'Are you sure you want to leave 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 that it uses the default message.