【问题标题】:Browsers continue to perform Javascript after "PageUnload" & new "PageLoad"浏览器在“PageUnload”和新的“PageLoad”之后继续执行 Javascript
【发布时间】:2015-04-26 15:45:41
【问题描述】:

我们有以下 AJAX 节流器。这是为了能够对一个页面执行许多 (20+) 个 ajax 请求,而不会因为第一个 X 请求总共花费了 60 秒而导致其余的超时。

RequestThrottler: {
    maximumConcurrentRequests: 3, //default to 3        
    requestQueue: new Array(),
    numberOfRequestCurrentlyProcessing: 0,

    addRequestToQueue: function (currentRequest) {
        var self = this;
        self.requestQueue.push(currentRequest);

        if (self.numberOfRequestCurrentlyProcessing < self.maximumConcurrentRequests) { self.sendNextRequest(); }
    },

    sendNextRequest: function () {
        var self = this;
        if (self.numberOfRequestCurrentlyProcessing >= self.maximumConcurrentRequests) { return; }
        if (self.requestQueue.length === 0) { return; }

        var currentRequest = self.requestQueue.pop();
        self.numberOfRequestCurrentlyProcessing++;
        AJAX.SendAjaxRequest(currentRequest.url, currentRequest.httpMethod, 
            function(data){
                self.numberOfRequestCurrentlyProcessing--;
                currentRequest.onSuccessCallback(data);
                self.sendNextRequest();
            }, 
            function(){
                self.numberOfRequestCurrentlyProcessing--;
                currentRequest.onErrorCallback();
                self.sendNextRequest();
            });
    },

    sendUpdateRequest: function (currentRequest) {
        var self = this;
        self.addRequestToQueue(currentRequest);
    }
}

但是,由于这些请求位于 Javascript 队列中,因此当用户尝试加载新页面时,开发人员工具会在新页面的 NET 区域中显示响应。我们的应用程序出于隐私原因进行了检查,以不允许此类行为。这对于浏览器来说是正常的,还是某种错误,还是我做错了什么?

【问题讨论】:

    标签: javascript ajax google-chrome firefox internet-explorer-11


    【解决方案1】:

    一个干净的解决方案是监听window.onbeforeunload 事件到abort 任何尚未收到响应的ajax 请求。

    应该使用beforeunload 事件而不是unload,原因如下:

    1)beforeunload事件比unload事件更可靠:

    卸载事件的确切处理因版本而异 浏览器的版本。例如,某些版本的 Firefox 触发 跟随链接时的事件,但不是在关闭窗口时。在 实际使用,应在所有支持的浏览器上测试行为, 并与专有的 beforeunload 事件形成对比。

    来源:

    2) beforeunload 事件可以取消,而unload 事件不能取消。如果您想在 beforeunload 事件发生时提示用户,这将为您提供灵活性。确认将询问用户是否要继续导航到其他页面,或者是否要取消,因为并非所有 ajax 请求都已完成。

    window.addEventListener("beforeunload", function (e) {
      var confirmationMessage = "\o/";
    
      (e || window.event).returnValue = confirmationMessage;     // Gecko and Trident
      return confirmationMessage;                                // Gecko and WebKit
    });
    

    来源:

    【讨论】:

    • 为什么选择onbeforeunload而不是onunload?
    • 我应该注意到我有另一个函数绑定到 before unload 事件并且注意到它在其他事件发生时触发(例如文件下载和 Flash 播放器启动)在这种情况下我不想要我的 ajax 请求退出。
    • 为了防止文件下载触发 beforeunload 事件,我找到了这个解决方案:stackoverflow.com/questions/14391531/…
    • 我找不到任何提到 Flash 播放器导致事件触发的内容。一种可能性是查看传递给 beforeunload 回调的 evt 对象,以查看是否有任何内容指定它来自 Flash 播放器并相应地执行某些操作。
    • 关于flash player,简单的监控beforeunload不会造成任何问题。您之前提到它正在触发 beforeunload 事件。由于 Flash 播放器非常具体,我觉得它脱离了原始问题的主题,你能为此创建另一个问题吗?
    猜你喜欢
    • 1970-01-01
    • 2011-12-29
    • 2012-05-21
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    • 1970-01-01
    相关资源
    最近更新 更多