How to replace a document in a window?

var newDoc = document.implementation.createHTMLDocument('someTitle');
// swap newDoc with document

DOMImplementation.createHTMLDocument

  • Can I replace the current document with a new document?
  • Are there any reasonable grounds for this?
+5
source share
3 answers

You cannot replace the current document object or any document object Documentwith an object created using the method createHTMLDocument.

createHTMLDocumentwas first introduced in one of the drafts DOM Level 2 Core, but was later removed from the final recommendation.

It was later added to the HTML5 specification because there was no programmatic way to create an HTML document.

Some of the options for using software to create an HTML document were

  • Create an unpublished HTML document for upload via XMLHttpRequest (instead of submitting an XML document).

  • HTML DOM , .

  • , , DOM, .

  • HTML5 JavaScript .

    iframe, . W3C

W3C, , [ 7842] : HTML- - createHTMLDocument

+5

document , DOM, document.cookie, location URL. , , window document.

, , document documentElement document documentElement. , . *

document.replaceChild(
    document.importNode(newdoc.documentElement, true),
    document.documentElement
);

, , iframe.

* , , doctype node doctype node .

+2

HTML, document.open, document.write document.close.

Quirks , <!doctype html>.

: http://jsbin.com/anusul/2

<html>    
    <script>
        alert('now in compatMode '+document.compatMode);
        if (document.compatMode==='BackCompat') {
            setTimeout(function() {
        var markup= '<!DOCTYPE html>New Page';
                document.open();
                document.write(markup);
                document.close();
            }, 0);
        }
    </script>
</html>​​​​​​

, .

source: Javascript quirksmode + : jquery prepend doctype to html

+1

All Articles