appendChild not working with window.open in IE

ria picture ria · Jun 10, 2013 · Viewed 9.9k times · Source

I have a page with an svg tag. The page has a button called "Preview" which on clicking should open a new window with the image (svg).

Below is a piece of code which works in Chrome/Firefox but not in IE (I'm using 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.

Answer

Mirko Cianfarani picture Mirko Cianfarani · Jun 10, 2013

IE will block appending any element created in a different window context from the window context that the element is being appending to.

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