Javascript 取消方法内的默认OnBeforUnload弹出消息
我有以下代码,我的问题是,有没有一种方法可以从方法调用内部取消onbeforeunload自定义弹出窗口?如果是firefox,我想显示我自己的自定义消息,如果用户按下cancel,我不想显示默认消息Javascript 取消方法内的默认OnBeforUnload弹出消息,javascript,html,Javascript,Html,我有以下代码,我的问题是,有没有一种方法可以从方法调用内部取消onbeforeunload自定义弹出窗口?如果是firefox,我想显示我自己的自定义消息,如果用户按下cancel,我不想显示默认消息 <!DOCTYPE html> <html> <head> <script type="text/javascript"> var clicked = false; function copyText() { var span = docu
<!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>
var=false;
函数copyText()
{
var span=document.getElementById('sp');
如果(单击==false)
{
单击=真;
appendChild(document.createTextNode(“单击”));
}
其他的
{
单击=假;
span.removeChild(span.firstChild);
}
}
window.onbeforeunload=函数(){
如果(单击==true)
返回;
其他的
{
if(navigator.userAgent.indexOf(“Firefox”)!=-1)
{
如果(确认(“是否确实要关闭窗口而不提交文档?”){
self.close();
return;//我不想在这里调用自定义弹出窗口
}
}
return“是否确实要关闭窗口而不提交文档?”;
}
};
//}
点击
这是我正在使用的代码:
window.onbeforeunload = function (e) {
if (clicked != true) {
e = e || window.event;
if (e) {
e.returnValue = 'Are you sure you want to close the window without submitting the documents?';
}
return 'Are you sure you want to close the window without submitting the documents?';
}
}
无法从函数内部取消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?";
}
};
这在firefox上不起作用,因为firefox在onbeforeunload函数中隐藏了自定义消息。我得到这个
页面要求您确认是否要离开-您输入的数据可能无法保存。
firefox 12.0谢谢,我想这是我唯一能做的事情!