appendChild不能处理窗口.在IE中打开

appendChild not working with window.open in IE

本文关键字:IE 不能 处理 窗口 appendChild      更新时间:2023-09-26

我有一个带有svg标记的页面。该页面有一个名为"预览"的按钮,点击该按钮将打开一个带有图像(svg)的新窗口。

下面是一段可以在Chrome/Firefox中工作但不能在IE中工作的代码(我使用IE9 - IE9标准模式)

var w = window.open();
var svg = $('#chart');              
var svgPrint = svg.cloneNode(true);
svgPrint.setAttribute('xmlns','http://www.w3.org/2000/svg');
w.document.body.appendChild(svgPrint);

任何建议都将是非常感谢的。

谢谢。

IE将阻止添加任何在不同窗口上下文中创建的元素,而不是该元素正在添加的窗口上下文。

var childWindow = window.open('somepage.html');
//will throw the exception in IE
childWindow.document.body.appendChild(document.createElement('div'));
//will not throw exception in IE
childWindow.document.body.appendChild(childWindow.document.createElement('div'));

在处理了相同的问题之后,这是在我的IE案例中工作的解决方案的摘录,避免了SCRIPT5022错误。感谢这篇文章的帮助

var myWindow = window.open('about:blank', 'loading...', '');
var myWindowDoc = myWindow.document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null);
var myWindowBody = myWindow.document.createElementNS('http://www.w3.org/1999/xhtml', 'body');
myWindow.document.open().write('<html><head></head><body><div id="targetDiv"></div></body></html>');
myWindow.document.close();
try {
    myWindow.document.getElementById('targetDiv').appendChild(HTMLpayload.cloneNode(true)); 
} catch (e){
    if (HTMLpayload.outerHTML) {
        myWindow.document.getElementById('targetDiv').innerHTML = HTMLpayload.outerHTML;
    } else {
        console.log(e);
    }
}