javascript - 取消方法内默认的onbefoarunload弹窗消息

标签 javascript html

我有以下代码,我的问题是,有没有一种方法可以取消方法调用内部的 onbeforeunload 自定义弹出窗口?如果是 firefox,我想显示我自己的自定义消息,如果用户按下取消,我不想显示默认消息。

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var clicked = false;

function copyText()
{
    var span = document.getElementById('sp');
    if(clicked == false)
    {   
        clicked = true;
        span.appendChild( document.createTextNode("clicked") );
    }
    else
    {
        clicked = false;
        span.removeChild( span.firstChild );
    }
}



window.onbeforeunload = function () { 

if(clicked == true)
return ; 

else 
{
if(navigator.userAgent.indexOf("Firefox")!=-1)
{ 
if(confirm("Are you sure you want to close the window without submitting the documents?")){
          self.close();
          return; // I want to not call the custom pop up here 
    }
}
    return "Are you sure you want to close the window without submitting the documents?";

}
};
//}



</script>
</head>
<!--<body onunload="confirmMe()>-->
<body>

<button onclick="copyText()">Click!</button>
<br>
<span style="color:lightblue" id="sp"></span>



</body>
</html>

最佳答案

您无法从函数内部取消 onbeforunload 事件。 但是如果用户不想关闭页面,下面将显示默认的弹出窗口,如果用户想留在页面上,它只会在 Firefox 中发生。

window.onbeforeunload = function () { 

if(clicked == false) 
{
    if(navigator.userAgent.indexOf("Firefox")!=-1)
    { 
        if(confirm("Are you sure you want to close the window without submitting the documents ?")){
                self.close();   
                return;   
        }

    }
   return "Are you sure you want to close the window without submitting the documents?";
}

};

关于javascript - 取消方法内默认的onbefoarunload弹窗消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10887251/

相关文章:

javascript - 您如何处理子画面?为什么它们在较老的游戏中得到如此广泛的使用?

javascript - 是否可以在现代浏览器上执行 'JSON hijacking'?

javascript - 等待map完成运行以执行最终调用

javascript - 在 Button 标签内的 span 标签 onClick 上更改类

css - 如何格式化 <div> 中的文本以使用动画平滑滚动?

javascript - 为什么我的评论没有被发布?

javascript - HTML - PHP 中的 'onclick' 确认函数

javascript - 制作自定义CSS规则

html - 边框导致DIV溢出

javascript - 在 Undefined index PHP 上寻找此错误的解决方案