加载Blob URL时,新的Firefox标签停滞在“未初始化”状态

时间:2019-05-07 13:52:47

标签: javascript firefox xmlhttprequest

我有一个生成PDF的脚本,它返回JS用于创建Blob对象的数据,然后将其用于在新的浏览器选项卡中显示pdf。

DoPDF() {
  self = this;
  self.timestamp = new Date().toDateString('yyyy-MM-dd HH:mm:ss');

  let postData = JSON.stringify(self);

  console.log(postData);
  let xhr = new XMLHttpRequest(),
    newTab = window.open('', '_cert-of-destruct');
  xhr.open('POST', 'php/makeCert.php', true);
  // xhr.setRequestHeader("Content-Type", "x-www-form-urlencoded");
  xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
  xhr.send(postData);
  xhr.responseType = 'blob';
  xhr.onload = function (e) {
    if (this.status == 200) {
      // Note: .response instead of .responseText
      let blob = new Blob([this.response], { type: 'application/pdf' });

      function isReady() {
        console.log(newTab.document.readyState);
        if (newTab.document.readyState === 'complete') {
          newTab.location = URL.createObjectURL(blob);
          resetUI(false);
        }
        else {
          setTimeout(isReady, 500);
        }
      }
      isReady();
    }
  };
}

这在除Firefox之外的所有浏览器中均能正常工作。似乎Firefox的newTab.document.readyState从未改变过。总会创建一个新的选项卡,但readyState停留在未初始化的位置。如果重新加载了新标签,则Blob将按预期加载,并且一切都会继续。

是什么导致新选项卡以未初始化状态挂起?我该如何解决或防止这种情况?

0 个答案:

没有答案