Rendering an entire HTML response inside a div

  Kiến thức lập trình

I make a request, as a browser might, for some HTML:

<div id="container"></div>
...
el = document.getElementById("container")

fetch(url).then((response) => response.text()).then((html) => {

    // tried this
    el.appendChild(new DOMParser().parseFromString(html, 'text/html');

    // tried that
    el.insertAdjacentHTML('afterbegin', new DOMParser().parseFromString(html, 'text/html');

    // never really expected this to work
    el.innerHTML = html

});

The HTML content can contain big pages with script tags and all sorts (transpiled through React).

I know I could make the container an iframe, but there are security concerns I want to avoid.

Any ideas?

1

LEAVE A COMMENT