【问题标题】:Getting confirmation box on browser close在浏览器关闭时获取确认框
【发布时间】:2011-01-12 09:41:27
【问题描述】:

出于上述目的,我尝试了以下代码,但这在 Mozilla 中不起作用,而且我在确认框中得到了一些额外的行:

<html>
<head>    
<meta http-equiv="Content-type" content="text/html; charset=utf-8">    <title>Coordinates!</title>
<script type="text/javascript">
 var myclose = false;
 function ConfirmClose()         
 {                 
  if (event.clientY < 0)
   {                         
   event.returnValue = 'Any message you want';
   setTimeout('myclose=false',100);
   myclose=true;                 
}         
}          
function HandleOnClose()         
{                 
if (myclose==true) 
alert("Window is closed");         
} 
</script>


  </head>  <body onbeforeunload="ConfirmClose()" onunload="HandleOnClose()" ><a href="#">test</a> </body></html>

您能否为 Mozilla 推荐一些东西?

【问题讨论】:

  • 请修正格式和拼写。

标签: javascript firefox dom-events


【解决方案1】:

如果您尝试在用户尝试离开页面时显示弹出窗口,这是可能的,甚至还有跨浏览器解决方案。但是,如果您尝试做的是从 javascript 调用以查看 firefox 是否正在退出,我认为这不可能从 javascript...那么我可能又错了。

function goodbye(e) {
    if(!e) e = window.event;
    //e.cancelBubble is supported by IE - this will kill the bubbling process.
    e.cancelBubble = true;
    e.returnValue = 'You sure you want to leave?';

    //e.stopPropagation works in Firefox.
    if (e.stopPropagation) {
        e.stopPropagation();
        e.preventDefault();
    }
}
window.onbeforeunload=goodbye;

source

【讨论】:

    【解决方案2】:

    您可以使用 jQuery 实现相同的目的。

    检查this 示例。在文本框关闭窗口中输入内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-18
      • 2010-11-04
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2014-09-28
      • 2020-12-16
      相关资源
      最近更新 更多