【问题标题】:Why does document.write() in AJAX overwrite the whole page before loading it? [duplicate]为什么 AJAX 中的 document.write() 会在加载之前覆盖整个页面? [复制]
【发布时间】:2020-11-21 16:58:05
【问题描述】:

为什么 AJAX 中的 document.write() 会在加载之前覆盖整个页面? 我的意思是它在 responseText 部分之前? AJAX 是如何加载网站的?

【问题讨论】:

  • 如果您正在学习以document.write 开头的书籍或教程……也许是时候寻找更好的了! document.write 不应该在现代 JavaScript 中使用。
  • 如果我想忽略网站的所有其他功能,有时我仍然使用document.write 来删除请求或反馈消息。

标签: javascript ajax


【解决方案1】:

如果您在已完成渲染的页面上使用document.write()(无论是否因为 AJAX 发生),之前的文档将被丢弃,并在其位置创建一个新文档。

来自MDN

注意:当 document.write 写入文档流时,调用 document.write 在已关闭(已加载)的文档上自动调用 document.open,这将清除文档。

document.write() 在这一点上被认为是遗留代码,并且仅在从代码(通常是动态创建的window)从头开始构建新文档时才真正有用。相反,采用现代方法,只需使用 AJAX 调用的结果更新现有的“包装器”元素。

【讨论】:

  • 好的。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2013-04-12
  • 2017-03-31
  • 2020-04-04
  • 2018-04-25
  • 2017-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多