var newDoc = document.implementation.createHTMLDocument('someTitle'); // swap newDoc with document
DOMImplementation.createHTMLDocument
- Is it possible to swap the current document for a new document?
- Is there any reasonable reason to do this?
Answer
You cannot replace the current document object or any document object with the Document
object created with createHTMLDocument
method.
The createHTMLDocument
was first introduced in one of the drafts of the DOM Level 2 Core
, but was later removed from the final recommendation.
It was later added to the HTML5 spec as there was no programmatic way to create an HTML document.
Some of the use cases provided for programmatic creation of an HTML document were,
Create a non-rendered HTML document to upload via XMLHttpRequest (instead of sending an XML document).
Feature-test the HTML DOM in library code in a way that is guaranteed to avoid side effects on the displayed document.
Create an isolated non-rendered document from a rich text editing area, so client-side cleanup can be done before uploading without disturbing the live DOM that the user may still edit further.
Implement HTML5 parsing algorithm client-side in JavaScript for testing and comparison purposes, or for virtualization or object-capability-based security.
An invisible iframe can be used for most of these purposes but that is more expensive in terms of resources. W3C mailing list
The conversation on W3C mailing lists that brought the method back into the spec, [Bug 7842] New: No programmatic way to make an HTML document – consider adding createHTMLDocument