为什么InternetExplorer 11不会在非活动选项卡中触发事件?

时间:2017-01-26 07:57:52

标签: javascript internet-explorer cross-browser

我遇到IE11问题:我在js中打开新窗口并在旧窗口中绑定一个事件。但是当旧窗口处于非活动状态时,它不起作用。当我快速切换标签时 - 工作,当我使用window.blur从新标签中删除焦点时它也可以工作。

有我的代码:

var win = window.open(url);
//win.blur(); trash solution
this.bindLoadEvent(win, function() {
     element.invoke();
});

bindLoadEvent: function (win, handler) {
     if (win.addEventListener) {
            win.addEventListener('load', handler, true);
     } else {
            win.attachEvent('onload', handler);
     }
}

似乎IE不会在非活动标签中触发事件。

P.S。我也在Firefox和Chrome中测试过它 - 它的工作效果很好。

1 个答案:

答案 0 :(得分:0)

在附加处理程序之前可能会发生加载事件。

在附加事件处理程序之前,尝试检查窗口是否已加载:

bindLoadEvent = function (win, handler) {
    if (win.document.readyState === "complete") {
      setTimeout(handler, 0);
    } else if (win.addEventListener) {
      win.addEventListener('load', handler, true);
    } else {
      win.attachEvent('onload', handler);
    }
  };

有关详情,请查看How to detect if document has loaded (IE 7/Firefox 3)Detecting the onload event of a window opened with window.open个问题。