DOMParser appending <script> tags to <head>/<body> but not executing
Asked Answered
C

3

10

I'm attempting to parse a string into a full HTML document via DOMParser and then overwrite the current page with the processed nodes. The string contains complete markup, including the <!doctype>, <html>, <head> and <body> nodes.

// parse the string into a DOMDocument element:
var parser = new DOMParser();
var doc = parser.parseFromString(data, 'text/html');

// set the parsed head/body innerHTML contents into the current page's innerHTML
document.getElementsByTagName('head')[0].innerHTML = doc.getElementsByTagName('head')[0].innerHTML;
document.getElementsByTagName('body')[0].innerHTML = doc.getElementsByTagName('body')[0].innerHTML;

This works in that it successfully takes the HTML nodes that were parsed and renders them on the page; however, any <script> tags that exist in either the <head> or <body> nodes inside the parsed string fail to execute =[. Testing directly with the html tag (opposed to head / body) yields the same result.

I've also tried to use .appendChild() instead of .innerHTML() too, but no change:

var elementHtml = document.getElementsByTagName('html')[0];

// remove the existing head/body nodes from the page
while (elementHtml.firstChild) elementHtml.removeChild(elementHtml.firstChild);

// append the parsed head/body tags to the existing html tag
elementHtml.appendChild(doc.getElementsByTagName('head')[0]);
elementHtml.appendChild(doc.getElementsByTagName('body')[0]);

Does anyone know of a way to convert a string to a full HTML page and have the javascript contained within it execute?

If there is an alternative to DOMParser that gives the same results (e.g. overwriting the full document), please feel free to recommend it/them =]

Note:
The reason I'm using this opposed to the much more simple alternative of document.write(data) is because I need to use this in a postMessage() callback in IE under SSL; document.write() is blocked in callback events such as post messages when accessing an SSL page in IE =[

Carbonari answered 8/4, 2014 at 18:57 Comment(3)
Have you already tried jQuery? Here you have the function $.parseHTML for parsing a HTML file. After adding a script tag via .append() should also execute it...Lorraine
@tampis Yeah, tried jQuery; whenever I pass in a full HTML document to parseHTML(), it seems to only parse the <head> for some reason; if you have a working example, please by all means let me see (I could have very easily messed up in testing =P)Carbonari
Related question: javascript - Why script elements created through DOMParser do not execute? - Stack OverflowDinge
C
10

Using DOMParser() as described in the question will correctly set the <head> and <body> contents of the page, but more work is necessary to get any existing <script> tags to execute.

The basic approach here is to pull a list of all <script> tags in the page after the contents have been set, iterate over that list and dynamically create a new <script> tag with the contents of the existing one and then add the new one to the page.

Example:

// create a DOMParser to parse the HTML content
var parser = new DOMParser();
var parsedDocument = parser.parseFromString(data, 'text/html');

// set the current page's <html> contents to the newly parsed <html> content
document.getElementsByTagName('html')[0].innerHTML = parsedDocument.getElementsByTagName('html')[0].innerHTML;

// get a list of all <script> tags in the new page
var tmpScripts = document.getElementsByTagName('script');
if (tmpScripts.length > 0) {
    // push all of the document's script tags into an array
    // (to prevent dom manipulation while iterating over dom nodes)
    var scripts = [];
    for (var i = 0; i < tmpScripts.length; i++) {
        scripts.push(tmpScripts[i]);
    }

    // iterate over all script tags and create a duplicate tags for each
    for (var i = 0; i < scripts.length; i++) {
        var s = document.createElement('script');
        s.innerHTML = scripts[i].innerHTML;

        // add the new node to the page
        scripts[i].parentNode.appendChild(s);

        // remove the original (non-executing) node from the page
        scripts[i].parentNode.removeChild(scripts[i]);
    }
}
Carbonari answered 10/4, 2014 at 20:2 Comment(2)
you could also use scripts[i].parentNode.replaceChild(scripts[i], s) instead, looks a bit better imo but thank youProbative
Also how would I deal with <script src=""> tags?Probative
D
13

You should use:

const sHtml = '<script>window.alert("Hello!")</script>';
const frag = document.createRange().createContextualFragment(sHtml)
document.body.appendChild( frag );
Demonstrator answered 14/11, 2019 at 17:23 Comment(0)
C
10

Using DOMParser() as described in the question will correctly set the <head> and <body> contents of the page, but more work is necessary to get any existing <script> tags to execute.

The basic approach here is to pull a list of all <script> tags in the page after the contents have been set, iterate over that list and dynamically create a new <script> tag with the contents of the existing one and then add the new one to the page.

Example:

// create a DOMParser to parse the HTML content
var parser = new DOMParser();
var parsedDocument = parser.parseFromString(data, 'text/html');

// set the current page's <html> contents to the newly parsed <html> content
document.getElementsByTagName('html')[0].innerHTML = parsedDocument.getElementsByTagName('html')[0].innerHTML;

// get a list of all <script> tags in the new page
var tmpScripts = document.getElementsByTagName('script');
if (tmpScripts.length > 0) {
    // push all of the document's script tags into an array
    // (to prevent dom manipulation while iterating over dom nodes)
    var scripts = [];
    for (var i = 0; i < tmpScripts.length; i++) {
        scripts.push(tmpScripts[i]);
    }

    // iterate over all script tags and create a duplicate tags for each
    for (var i = 0; i < scripts.length; i++) {
        var s = document.createElement('script');
        s.innerHTML = scripts[i].innerHTML;

        // add the new node to the page
        scripts[i].parentNode.appendChild(s);

        // remove the original (non-executing) node from the page
        scripts[i].parentNode.removeChild(scripts[i]);
    }
}
Carbonari answered 10/4, 2014 at 20:2 Comment(2)
you could also use scripts[i].parentNode.replaceChild(scripts[i], s) instead, looks a bit better imo but thank youProbative
Also how would I deal with <script src=""> tags?Probative
L
0

Here is a working demo for jQuery 1.8.3 (link to jsFiddle):

var html = "<html><head><script>alert(42);</" + "script></head><body><h1>Hello World</h1></body></html>";

$(function () {
    html = $($.parseXML(html));

    $("head").append(html.find("script"));
    $("body").append(html.find("h1"));
});

Thereby I used the function $.parseXML() which you only can use obviously, if your HTML is also valid XML. Unfortunately is the same code not working with jQuery 1.9.1 (the <script> tag is not found anymore): http://jsfiddle.net/6cECR/8/ Maybe its a bug (or a security fetaure...)

Lorraine answered 9/4, 2014 at 16:43 Comment(3)
I need to append the full contents of the <head> and <body> tags; this approach doesn't work with that - it works fine if I append individual tags (like <h1> or <script>), but then again so does raw javascript (i.e. not jQuery) and individually appending 10k+ nested tags isn't efficient enough to consider this a valid approach =/Carbonari
jsfiddle.net/6cECR/10 but it is buggy... I will work again on this topic later...Lorraine
Sorry, but this one doesn't work for me... the dependency of an older version of jQuery is not really a good solution. Based on that alone I haven't tested to see if it actually works in the scenario I require - Internet Explorer over SSL being used in a cross-origin callback function.Carbonari

© 2022 - 2024 — McMap. All rights reserved.