appendChild not working with window.open in IE
Asked Answered
B

2

11

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.

Butterfingers answered 10/6, 2013 at 6:2 Comment(6)
I suppose about:blank is run in Quirks mode, which doesn't support svg.Thirtytwo
@Thirtytwo It is being run in IE9 standards mode...Butterfingers
Yes, your main page is, but the window you open is more likely to run in Quirks mode, since it doesn't have a doctype declaration... Just test it by opening a real document in it.Thirtytwo
@Thirtytwo The above code doesn't work for other html elements also.. :( I tried opening new window with a html div which contains just a Text. Even that doesn't work.. I get a blank page.Butterfingers
Weird, IE throws SCRIPT5022 error (Exception thrown and not caught). But this code has nothing to do with try..catch ?! Also loading a real page to a newly opened window doesn't remove this error. Setting w.document.body.innerHTML = '...' seems to work. When enclosing appendChild() within try..catch, the error message is HierarchyRequestError, which means, that "The node cannot be inserted at the requested location." This smells a big bug in IE...Thirtytwo
@Butterfingers look on my answer please...Laise
L
14

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'));
Laise answered 10/6, 2013 at 7:0 Comment(3)
I tried this.. but I'm getting this error: DOM Exception: HIERARCHY_REQUEST_ERR (3)Butterfingers
Also where you have this error (sentence or part of code please)Laise
Thanks guys! Creating the nodes in the same context I'm appending to solved the problem :)Teryn
S
1

After dealing with the same issue, this is an excerpt of the solution that worked in my case for IE, avoiding the SCRIPT5022 error. 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);
    }
}
Stagecraft answered 9/7, 2015 at 2:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.