DOMImplementation.createHTMLDocument

  • Revision slug: DOM/DOMImplementation.createHTMLDocument
  • Revision title: DOMImplementation.createHTMLDocument
  • Revision id: 37361
  • Created:
  • Creator: ziyunfei
  • Is current revision? No
  • Comment 1 words added

Revision Content

{{ gecko_minversion_header("2.0") }}

Summary

This method (available from document.implementation) creates a new HTML {{ domxref("document") }}.

Syntax

var doc = document.implementation.createHTMLDocument(title);
  • doc is the DOM {{ domxref("document") }} returned by the method
  • title is the title to give the new HTML document

Example

This example creates a new HTML document and inserts it into an {{ HTMLElement("iframe") }} in the current document.

{{ DOMLiveSample("createHTMLDocument.html") }}

Here's the HTML for this example:

<body>
  <p>Click <a href="javascript:makeDocument()">here</a> to create a new document and insert it below.</p>
  <iframe id="theFrame" src="about:blank" />
</body>

The JavaScript implementation of the makeDocument() method follows:

function makeDocument() {
  var frame = document.getElementById("theFrame");
          
  var doc = document.implementation.createHTMLDocument("New Document");
  var p = doc.createElement("p");
  p.innerHTML = "This is a new paragraph.";
  
  try {
    doc.body.appendChild(p);
  } catch(e) {
    console.log(e);
  }

  // Copy the new HTML document into the frame

  var destDocument = frame.contentDocument;
  var srcNode = doc.documentElement;
  var newNode = destDocument.importNode(srcNode, true);
  
  destDocument.replaceChild(newNode, destDocument.documentElement);
}

The code in lines 4-12 handle creating the new HTML document and inserting some content into it. Line 4 uses createHTMLDocument() to construct a new HTML document whose {{ HTMLElement("title") }} is "New Document". Lines 5 and 6 create a new paragraph element with some simple content, and then lines 8-12 handle inserting the new paragraph into the new document.

Line 16 pulls the contentDocument of the frame; this is the document into which we'll be injecting the new content. The next two lines handle importing the contents of our new document into the new document's context. Finally, line 20 actually replaces the contents of the frame with the new document's contents.

Remarks

The returned document is pre-constructed with the following HTML:

<!doctype html>
<html>
<head>
<title>title</title>
</head>
<body>
</body>
</html>

Specification

{{ languages( { "zh-cn": "zh-cn/DOM/DOMImplementation.createHTMLDocument" } ) }}

Revision Source

<p>{{ gecko_minversion_header("2.0") }}</p>
<h3 id="Summary">Summary</h3>
<p>This method (available from <a class="internal" href="/en/DOM/document.implementation" title="En/DOM/Document.implementation"><code>document.implementation</code></a>) creates a new HTML {{ domxref("document") }}.</p>
<h3 id="Syntax">Syntax</h3>
<pre>var doc = document.implementation.createHTMLDocument(title);
</pre>
<ul> <li><code>doc</code> is the DOM {{ domxref("document") }} returned by the method</li> <li><code>title</code> is the title to give the new HTML document</li>
</ul>
<h3 id="Example">Example</h3>
<p>This example creates a new HTML document and inserts it into an {{ HTMLElement("iframe") }} in the current document.</p>
<p>{{ DOMLiveSample("createHTMLDocument.html") }}</p>
<p>Here's the HTML for this example:</p>
<pre class="brush: html">&lt;body&gt;
  &lt;p&gt;Click &lt;a href="javascript:makeDocument()"&gt;here&lt;/a&gt; to create a new document and insert it below.&lt;/p&gt;
  &lt;iframe id="theFrame" src="about:blank" /&gt;
&lt;/body&gt;
</pre>
<p>The JavaScript implementation of the <code>makeDocument()</code> method follows:</p>
<pre class="brush: js">function makeDocument() {
  var frame = document.getElementById("theFrame");
          
  var doc = document.implementation.createHTMLDocument("New Document");
  var p = doc.createElement("p");
  p.innerHTML = "This is a new paragraph.";
  
  try {
    doc.body.appendChild(p);
  } catch(e) {
    console.log(e);
  }

  // Copy the new HTML document into the frame

  var destDocument = frame.contentDocument;
  var srcNode = doc.documentElement;
  var newNode = destDocument.importNode(srcNode, true);
  
  destDocument.replaceChild(newNode, destDocument.documentElement);
}
</pre>
<p>The code in lines 4-12 handle creating the new HTML document and inserting some content into it. Line 4 uses <code>createHTMLDocument()</code> to construct a new HTML document whose {{ HTMLElement("title") }} is "New Document". Lines 5 and 6 create a new paragraph element with some simple content, and then lines 8-12 handle inserting the new paragraph into the new document.</p>
<p>Line 16 pulls the <code>contentDocument</code> of the frame; this is the document into which we'll be injecting the new content. The next two lines handle importing the contents of our new document into the new document's context. Finally, line 20 actually replaces the contents of the frame with the new document's contents.</p>
<h3 id="Remarks">Remarks</h3>
<p>The returned document is pre-constructed with the following HTML:</p>
<pre class="brush: html">&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;<em>title</em>&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<h3 id="Specification">Specification</h3>
<ul> <li><a class="external" href="http://www.whatwg.org/html/#dom-domhtmlimplementation-createhtmldocument" title="http://www.whatwg.org/html/#dom-domhtmlimplementation-createhtmldocument"><code>createHTMLDocument</code> specification</a></li>
</ul>
<p>{{ languages( { "zh-cn": "zh-cn/DOM/DOMImplementation.createHTMLDocument" } ) }}</p>
Revert to this revision