I need to write an html string to an element and then read the resulting outer html from that element. I need this to work in IE10, latest FF, Chrome, Safari, Android, iOS Safari but not in any older browsers. In all non-ie browsers the following simple approach works:
var html = WIUI.createElement('html');
html.innerHTML = htmlString;
console.log(html.outerHTML);
However in IE10 the above approach fails in one unacceptable way. Somehow the html element has a body tag matching that of the parent document, (NOT of the html string I give it!!! I assume this is a crazy bug in the browser itself). You can see this bug in action here: https://mod.it/iuu_1DcT, if you view that application in a browser besides IE10 the output body onload function will match the input body onload function. In IE10 it will set the output onload function to foo() no matter what the input is because foo is the onload function of the parent.
An alternative approach that does work in IE10 (and all modern browsers) is to create and iframe like so:
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
var doc = iframe.contentWindow.document;
doc.open();
doc.write(htmlString);
doc.close();
console.log(doc.documentElement.outerHTML);
However this has the unfortunate side effect that writing to the iframe actually causes the html to be executed which I do not want.
From my research something like so SHOULD work (and does in browsers that aren't IE)
var doc = document.implementation.createHTMLDocument('temp');
doc.open();
doc.write(htmlString);
doc.close();
console.log(doc.documentElement.outerHTML);
However in IE10 the doc.open line gives an "unspecified error". Can anyone give me any clue what is going on or why IE is so difficult to work with compared to other browsers for this type of task?