Thursday, April 25, 2024
138
rated 0 times [  144] [ 6]  / answers: 1 / hits: 19936  / 11 Years ago, tue, november 19, 2013, 12:00:00

I just want to show a message before leaving the page, but my code doesn't works:



window.onload=function(){
alert(Page with a digital clock);
setInterval(clock,1000);
}

window.onbeforeunload=function(){
alert(Are you sure to leave this page?);
}


The onload alert works fine, but the second is not working..


More From » onbeforeunload

 Answers
11

You can't put an alert inside onbeforeunload. Most browsers will do this for you so you don't need to handle it, you need to return the confirm message to the method instead:



window.onbeforeunload=function(){
return Are you sure to leave this page?;
}


Here are the docs for the method on MDN.




When this event returns a non-void value, the user is prompted to
confirm the page unload. In most browsers, the return value of the
event is displayed in this dialog



[#74197] Monday, November 18, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alorac

Total Points: 262
Total Questions: 82
Total Answers: 97

Location: Libya
Member since Mon, Dec 7, 2020
3 Years ago
alorac questions
Sat, Oct 10, 20, 00:00, 4 Years ago
Tue, Sep 22, 20, 00:00, 4 Years ago
Wed, Jul 1, 20, 00:00, 4 Years ago
Wed, Jun 3, 20, 00:00, 4 Years ago
Sun, May 17, 20, 00:00, 4 Years ago
;