【问题标题】:write() on a document crashes internet explorer 11文档上的 write() 使 Internet Explorer 11 崩溃
【发布时间】:2014-12-19 22:19:22
【问题描述】:

以下代码在 chrome 和 Safari 中有效,但在 IE11 中使网站崩溃:

var doc = document.implementation.createHTMLDocument("");
doc.open("replace");
doc.write(document.querySelector("html").outerHTML);
doc.close()

我正在尝试做的是创建 DOM 的克隆(不加载脚本/图像等)以进行操作。知道为什么这会使 IE 崩溃吗?有一个更好的方法吗?我正在为 outerHTML 使用 polyfill(尽管我认为它在 IE11 中受支持)并且可以确认 outerHTML 可以按预期工作。

提前谢谢你!

【问题讨论】:

  • 是否有可能,代码在 IE 中创建了一种无限递归? outerHTML 是什么,即使在 IE5 中也原生支持 ...
  • 你考虑过使用cloneNode吗?为什么这个方法不会加载图片和脚本?
  • 我实际上不知道为什么它不会,我只是在某个地方读到它。我想我可能已经找到了使用 dpcumentfragments 的解决方案,但仍在尝试。

标签: javascript internet-explorer crash internet-explorer-11


【解决方案1】:

这似乎不是导致 IE11 崩溃的 write() 方法,而是与 close() 相关。在类似问题上找到的最佳答案 Why won't this JavaScript (using document.open and document.write) work in Internet Explorer or Opera? 发现不包括 close() 调用阻止了 IE 崩溃。

我自己也有类似的问题:

var doc = document.implementation.createHTMLDocument('');
doc.open();
doc.write('<body><p>Hello world</p>');
doc.close(); // This is where it breaks

但是,在不破坏页面的情况下,我实际上没有任何运气调用该方法。我尝试将其添加到超时或仅在完成 ​​DOM 时才关闭,但似乎都失败了。我猜是检测到IE11而不调用close的情况。

if (!(window.ActiveXObject) && "ActiveXObject" in window) {
    // IE11, do nothing... may cause memory leaks...
} else {
    doc.close();
}

我只尝试过 IE11,但它也可能在早期版本的 IE 中崩溃……在这种情况下使用

if ("ActiveXObject" in window) {
    // IE, do nothing... may cause memory leaks...
} else {
    doc.close();
}

希望这对遇到同样问题的人有所帮助。

【讨论】:

    猜你喜欢
    • 2017-07-23
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 2013-12-23
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多