开发者

JavaScript: trying to exit onbeforeunload function in an if statement

开发者 https://www.devze.com 2023-03-14 09:00 出处:网络
I have the following JavaScript code at the head section of the page: window.onbeforeunload =function()

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

精彩评论

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