Yesterday I posted a different (related) question:
CSS height 100% behaves different when embedded in iframe
The answer to that led me to look at doctypes as a potential cause for what was happening. However I am now starting to think that something more sinister is going on.
When creating an iframe node by node and setting a doctype with
document.implementation.createDocumentType("html", "", "")
<iframe></iframe> creates a document. Since there is no doctype, this is committed to use quirks mode. Adding a documentType node to the document doesn't change this commitment.
document.write() code blows away the old document and creates a new one. Writing
<!DOCTYPE html> at the start puts the new document into standards mode.