开发者

JavaScript: trying to exit onbeforeunload function in an if statement

I have the following JavaScript code at the head section of the page:

window.onbeforeunload =  function()
{var r=confirm("R开发者_如何学Ceally leave?");
if (r==true)
{
self.close;
}
else
{
break;
}} 

After 'else,' I want code that will cancel the onbeforeunload so that when the user clicks 'cancel,' the webpage should not exit. Can this be done?


You can't modify the behavior of the browser's default onbeforeunload dialogue because of security reasons.

Let the dialogue handle the 'Really leave?' logic for you (it will generate the button logic).

The only thing you can modify is the message itself (in some browsers only partial) and a logic if the window should appear at all (e.g. only if the user made changes):

window.onbeforeunload = function() {
   return 'Your custom message why the user should not leave';
}

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜