Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I load an entire HTML document into a document fragment in Internet Explorer?
    text
    copied!<p>Here's something I've been having a little bit of difficulty with. I have a local client-side script that needs to allow a user to fetch a remote web page and search that resulting page for forms. In order to do this (without regex), I need to parse the document into a fully traversable DOM object.</p> <p>Some limitations I'd like to stress:</p> <ul> <li>I don't want to use libraries (like jQuery). There's too much bloat for what I need to do here.</li> <li>Under no circumstances should scripts from the remote page be executed (for security reasons).</li> <li>DOM APIs, such as <code>getElementsByTagName</code>, need to be available.</li> <li>It only needs to work in Internet Explorer, but in 7 at the very least.</li> <li>Let's pretend I don't have access to a server. I do, but I can't use it for this.</li> </ul> <h2>What I've tried</h2> <p>Assuming I have a complete HTML document string (including DOCTYPE declaration) in the variable <code>html</code>, here's what I've tried so far:</p> <pre><code>var frag = document.createDocumentFragment(), div = frag.appendChild(document.createElement("div")); div.outerHTML = html; //-&gt; results in an empty fragment div.insertAdjacentHTML("afterEnd", html); //-&gt; HTML is not added to the fragment div.innerHTML = html; //-&gt; Error (expected, but I tried it anyway) var doc = new ActiveXObject("htmlfile"); doc.write(html); doc.close(); //-&gt; JavaScript executes </code></pre> <p>I've also tried extracting the <code>&lt;head&gt;</code> and <code>&lt;body&gt;</code>nodes from the HTML and adding them to a <code>&lt;HTML&gt;</code> element inside the fragment, still no luck.</p> <p>Does anyone have any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload