AppendChild does not work with window.open in IE

I have a page with svg tag. On the page there is a button called "Preview", which when clicked should open a new window with the image (svg).

Below is a snippet of code that works in Chrome / Firefox but not in IE (I use IE 9-IE9 standards mode)

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.

+6
javascript internet-explorer appendchild
source share
2 answers

IE blocks the addition of any element created in a different window context from the context of the window to which the element is added.

 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')); 
+10
source share

After solving the same problem, this is a fragment of a solution that worked in my case for IE, avoiding error SCRIPT5022. Thanks to the help of 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); } } 
0
source share

All Articles