【问题标题】:Javascript to detect and send a message to server if the window is closedJavascript 在窗口关闭时检测并向服务器发送消息
【发布时间】:2012-02-24 16:18:45
【问题描述】:

我想知道窗口关闭确认向服务器发送关于窗口关闭的消息的答案是什么!

我找到了这个,但这不会返回用户答案。

window.onbeforeunload = function (evt) {
    var message = 'Are you sure?';
    if (typeof evt == 'undefined') {
        evt = window.event;
    }
    if (evt) {
        evt.returnValue = message;
        }
    return message;
};

【问题讨论】:

    标签: javascript jquery asp.net ajax onbeforeunload


    【解决方案1】:

    函数一返回,窗口就关闭。

    无论您想使用 message 执行什么操作,都必须在 onbeforeunload 处理程序中执行

    【讨论】:

    • 我想总结一下我的经验:在 beforeunload 中执行 AJAX 调用等操作可能会对某些浏览器造成严重破坏,尤其是 Internet Explorer。我不知道为什么,我们只是重新设计了应用程序工作流程以避免这些情况。
    【解决方案2】:

    已解决:)

    window.onunload = window.onbeforeunload = function (evt) {
        var message = 'Are you sure?';
        if (typeof evt == 'undefined') {
            evt = window.event;
        }
        if (evt) {
            if (evt.type == "unload" && evt.returnValue) {
    
                // ACTION WHICH SHOULD BE DONE ON CLOSE
    
            }
            evt.returnValue = message;
        }
        return message;
    };
    

    【讨论】:

      猜你喜欢
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多