javascript - 为什么文档的就绪状态从 "complete"更改为 "loading"?

标签 javascript html dom

请看下面的代码:

this.detachedWin = window.open(window.origin + "/@/tab","Detached","menubar=0,toolbar=0,personalbar=0,status=0");
  
this.doc = this.detachedWin.document;

setInterval(() => {
  console.log(this.doc === this.detachedWin.document,this.detachedWin.document.readyState);
  this.doc = this.detachedWin.document;
}, 10);

JSFiddle

如果您查看控制台日志,您会看到如下内容:

enter image description here

我不明白为什么文档的就绪状态第一次是“完成”,之后又变成加载状态的原因是什么? 我希望第一次准备状态为“正在加载”。

最佳答案

第一次“完成”发生在空白窗口中,然后将位置更改为提供的 URL。

Note that remote URLs won't load immediately. When window.open() returns, the window always contains about:blank. The actual fetching of the URL is deferred and starts after the current script block finishes executing. The window creation and the loading of the referenced resource are done asynchronously.

developer.mozilla.org

简化的窗口打开流程

  1. window.open()
  2. 立即加载,因为没有要加载的内容:“正在加载”
  3. 立即变为交互式,因为它是空的:“interactive”
  4. 空白窗口已准备就绪:“已完成”
  5. 显式或隐式设置window.location
  6. 重定向至网址:“正在加载”
  7. 窗口变为交互式:“交互式”
  8. 窗口已准备就绪:“已完成”

您可以在 specification 中找到打开窗口的所有步骤的完整流程。 .

要了解其工作原理,请查看示例:

const openMe = () => { 
  this.detachedWin = window.open("", "Detached","menubar=0,toolbar=0,personalbar=0,status=0");

  setTimeout(() => {
    console.log("loading happen now")
    this.detachedWin.location = window.origin + "/@/tab"
  }, 1000);

  this.doc = this.detachedWin.document;
  setInterval(() => {
    console.log(this.doc === this.detachedWin.document, this.detachedWin.document.readyState);
    this.doc = this.detachedWin.document;
  }, 10);
}
<button onclick="openMe()">Open window</button>

关于javascript - 为什么文档的就绪状态从 "complete"更改为 "loading"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65407620/

相关文章:

javascript - 关于load函数和json的问题

javascript - 如何获取 DIV 的值

javascript - 如何在 Joi 验证器中添加常量?

javascript - div值改变时执行javascript函数

javascript - 无法使用 createElement 为 div 标签创建 anchor ?

javascript - 使用 Javascript while 循环将文本重复输出到 .innerHTML

javascript - 带有预览和删除,Jquery和Javascript的多图像上传

php - 当从 select 标记触发 onChange 事件时,从 mysql 数据库加载 html 表

java - 如何使用 JavaMail 发送 html 电子邮件?

java - 将java的xml Element对象转换为文本