刷新 URL 时,打开卸载确认警报两次

Getting onbeforeunload confirmation alert twice, when refreshing the URL?

本文关键字:两次 确认 URL 卸载 刷新      更新时间:2023-09-26

当用户离开页面时,我正在使用window.onbeforeunload进行用户确认,但确认警报仅在IE中发生两次。

我的代码是:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User confirmation</title>
<script type="text/javascript">
    /* Used for confirmation , to closing the window */
    window.onbeforeunload = function() {
        return "Are you sure want to LOGOUT the session ?";
    };
</script>
</head>
<body>
<p>Folow the steps which I said in Stack overflow</p>
</body>
</html>

场景是 ,

步骤1:运行我的代码并在IE中打开它。

步骤2:应用程序加载后,关闭浏览器窗口。您将收到确认警报。

步骤3:在确认警报中取消。

步骤4:然后按 输入 地址栏中的按钮

第 5 步:在警报中给出确定。

步骤6:现在您将再次发出确认警报。

谁能告诉我如何解决这个问题?

这是

IE中的设计使然。

阅读列出引发该事件的所有内容的备注。

http://msdn.microsoft.com/en-us/library/ie/ms536907%28v=vs.85%29.aspx

您可以使用计时器来避免这种情况:

window.onbeforeunload 可能会触发多次