2

Because I am a glutton for embarrassment, I have another in my series of stupid questions about synthetic documents in JavaScript.

So here goes: createDocument, createHTMLDocument, new Document() and DOMParser.parseFromString() all return new documents. While the first three all return complete documents, documents returned by DOMParser.parseFromString() are uninitialized. Any idea why? (I'm betting on some stupid JavaScript error on my part.)

Here's some code if you want to test:

<script>
function doit() {
  var doc1 = document.implementation.createDocument (null, 'html', null);
  console.log(doc1.readyState); // complete

  var doc2 = document.implementation.createHTMLDocument("HTML Doc");
  console.log(doc2.readyState); // complete

  var doc3 = new Document();
  console.log(doc3.readyState); // complete

  var parser = new DOMParser; 
  var doc4 = parser.parseFromString("<!doctype html>", "text/html");
  console.log(doc4.readyState); // uninitialized
}
window.onload = doit;
</script> 

ETA: Above are the results on Firefox. Chrome returns "interactive." Still not sure why.

P Jones
  • 1,077
  • 1
  • 9
  • 14
  • Just a guess, but the string could contain elements like `` that need to be loaded asynchronously, and it won't be ready until they're loaded. – Barmar Jan 27 '15 at 05:03
  • The string in this case is " ", so I'm not sure that that is the issue. – P Jones Jan 27 '15 at 05:18
  • But it **could** contain other things. So the interface always returns an unready document. – Barmar Jan 27 '15 at 05:21
  • Adding to the confusion, it looks like `doc4.readyState` is "complete" in IE11. – tiwahu Feb 10 '15 at 15:33

0 Answers0