以下代码可以在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按预期工作。
提前谢谢你!
发布于 2016-03-30 23:55:04
它似乎不是导致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();
}希望这篇文章能帮助任何遇到同样问题的人。
https://stackoverflow.com/questions/27574705
复制相似问题