简体   繁体   English

appendChild在IE中没有使用window.open

[英]appendChild not working with window.open in IE

I have a page with an svg tag. 我有一个带有svg标签的页面。 The page has a button called "Preview" which on clicking should open a new window with the image (svg). 该页面有一个名为“预览”的按钮,在点击时应该打开一个带有图像的新窗口(svg)。

Below is a piece of code which works in Chrome/Firefox but not in IE (I'm using IE 9- IE9 standards mode) 下面是一段代码,可以在Chrome / Firefox中使用,但不能在IE中使用(我使用的是IE 9-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);

Any suggestions would be highly appreciated. 任何建议都将受到高度赞赏。

Thanks. 谢谢。

IE will block appending any element created in a different window context from the window context that the element is being appending to. 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'));

After dealing with the same issue, this is an excerpt of the solution that worked in my case for IE, avoiding the SCRIPT5022 error. 在处理相同的问题之后,这是我在IE中的解决方案的摘录,避免了SCRIPT5022错误。 Thanks to help from this post . 感谢这篇文章的帮助。

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM