【问题标题】:onbeforeunload function created in Vaadin 7 returning empty string still displays prompt in IE 10在 Vaadin 7 中创建的 onbeforeunload 函数返回空字符串仍然在 IE 10 中显示提示
【发布时间】:2023-03-03 22:44:01
【问题描述】:

我不确定这是 Javascript 问题还是 Vaadin 问题,所以我认为最好在这里提问,而不是在 Vaadin 论坛上提问。

为了在浏览器关闭时(如果可能)立即清理资源,我在我的 Vaadin 应用程序中添加了以下代码:

JavaScript.getCurrent().addFunction("catchClose", new JavaScriptFunction() {
private static final long serialVersionUID = 1L;
@Override
public void call(JSONArray arguments) throws JSONException {
    VaadinSession.getCurrent().close();
    /*causes detach() to be called which does all my cleanup*/
}
});

Page.getCurrent().getJavaScript().execute(
    "window.addEventListener('beforeunload', "
                + "function() {"
                + "var e = window.event;"
                + "e.returnValue = '';"
                + "catchClose(); "
                + "return '';"
            + "}, false);"
    );

我也尝试过直接设置 onbeforeunload 函数,而不是像 Vaadin 论坛上的 this post 中提到的那样添加事件监听器。

在 Firefox 中,这按预期工作,事件被发送回服务器没有显示询问用户是否要离开页面的提示。就让他们离开吧。

但是,在 IE 10(和 Chrome,但现在只有 IE 10 相关)中,仍然会发送消息,这会关闭我的 Vaadin 会话,但会要求用户确认他们是否要离开。由于我已经关闭了会话,如果他们选择留下,这显然会引起一些大问题。

根据the IE documentation on the onbeforeunload event,如果我返回空值,则不应弹出对话框。除了返回一个空字符串,我还尝试返回nullfalse,它们都将提示的消息设置为“null”和“false”。我也尝试过完全不设置e.returnValue,将e.returnValue设置为nullfalse,以及上述尝试的各种排列组合。

所以简而言之,问题是我需要如何更改上述代码以防止“您确定要离开”提示在 IE 10 中显示? Javascript 有什么问题,还是我在 IE 文档中遗漏了什么?

谢谢!

【问题讨论】:

    标签: javascript internet-explorer vaadin onbeforeunload


    【解决方案1】:

    我猜返回一个空值是一种不清楚的方式来表示返回未定义......你的函数应该只是说return;没有值,return undefined;,或return expression;,其中表达式计算为未定义(例如window.vrenifbruwfgwvoid(0)等……)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      相关资源
      最近更新 更多