Forum Moderators: open

Message Too Old, No Replies

Custom onbeforeunload message

         

usrbin

2:22 am on Sep 6, 2016 (gmt 0)

5+ Year Member Top Contributors Of The Month



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.

blend27

1:09 pm on Sep 7, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



var lock = 0;

and
if(lock) return...

It seems like you are treating your VAR as a Boolean. Boolean being a part of a logical combinatorial system is either Yes or No, where 0 is always NO, anything that is not 0 is YES as long as it is numeric.

Ask the question in your code.

Is var == 2?