【问题标题】:Getting onbeforeunload confirmation alert twice, when refreshing the URL?刷新 URL 时收到两次 onbeforeunload 确认警报?
【发布时间】:2020-08-01 20:20:12
【问题描述】:

当用户离开页面时,我使用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 步:然后按地址栏中的 ENTER 按钮刷新 URL。

第 5 步:在警报中给出 ok

第 6 步:现在您将再次收到确认警报。

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

【问题讨论】:

  • 您尝试过计时器解决方法吗?

标签: javascript jsp internet-explorer dom-events


【解决方案1】:

这是 IE 设计的。

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

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

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

window.onbeforeunload may fire multiple times

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 2014-02-27
    相关资源
    最近更新 更多