javascript - 如何修复 "An unbalanced tree was written using document.write() causing data from the network to be reparsed"?

标签 javascript html

JS 错误:

An unbalanced tree was written using document.write() causing data from the network to be reparsed

<script type="text/javascript">
 beginAnchor("javascript:popupManager.close()");
</script>
<img src="../images/icons/icon_cancel.gif" border="0" alt="" title="">

<script type="text/javascript">
 endAnchor();
</script>

最佳答案

这不是错误,而是警告。它发生在 Firefox 4 及更高版本中。

来自MDN: Optimizing your pages for speculative parsing :

However, in Firefox 4 and later the HTML parser also runs the HTML tree construction algorithm speculatively. The upside is that when a speculation succeeds, there's no need to reparse the part of the incoming file that was already scanned for scripts, style sheets and images. The downside is that there's more work lost when the speculation fails.
[...]
Speculative tree building fails when document.write() changes the tree builder state such that the speculative state after the </script> tag no longer holds when all the content inserted by document.write() has been parsed. However, only unusual uses of document.write() cause trouble. Here are the things to avoid:
[...]
Don't write unbalanced trees. <script>document.write("<div>");</script> is bad. <script>document.write("<div></div>");</script> is OK.

解决方案是将 HTML 编写在单个 <script> 中如果您关心性能损失,请阻止。如果您愿意,也可以忽略它。

关于javascript - 如何修复 "An unbalanced tree was written using document.write() causing data from the network to be reparsed"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24731079/

相关文章:

javascript - 如何根据url中的变量显示图像

html - 自动完成标签是 .shtml 文件中的有效标签吗?

html - 导出为 PDF 时如何在文本字段中包含图像和表格?

javascript - 如何使 jQuery 对话框在 IE8 中的页面加载时不显示?

javascript - 是否可以使用 CSS 在 DOM 元素上为 SVG 背景设置动画?

javascript - Foundation 和 jquery Fullcalendar 问题

javascript - 只有名称时如何更改选定索引?

javascript - 重构 javascript 以避免撇号引起问题?

jquery - 从使用全宽背景图像的现有标记创建 Bootstrap 轮播

html - GWT SafeHtmlBuilder 如何在 div 中插入 html 代码