createHTMLDocument(), createDocument()

目次

概要

[object HTMLDocument], [object Document] を生成するメソッドのまとめです。
JavaScriptコードの説明はしてませんが、document.implementation.createHTMLDocument.js をみれば、多分きっとおそらくわか……ごめんなさい、石投げないで!

function XMLAnalysisXML1.0 に80%ぐらい準拠してます。残りの20%はあまりにも長い文字クラスで使われそうにない文字を削った分です。関数をグループ化しただけでオブジェクトっぽくないところは気が向いたら直します。

XHTMLString

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="ja" xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>sample1</title>
  <style type="text/css"><![CDATA[
    body { margin: 0; padding: 1em; }
  ]]></style>
</head>
<body><p>Hello, World!</p></body>
</html>

XHTMLStringを元に生成したdocument

各メソッドの対応ブラウザ

ブラウザ document.implementation createHTMLDocument() createDocument() createLSParser() DOMParser() ActiveXObject("htmlfile")
Google Chrome 5 対応 対応 対応 非対応 対応 非対応
Opera v10.60 対応 対応 対応 対応 対応 非対応
Firefox v3.6.6 対応 非対応 対応 非対応 対応 非対応
IE8 対応 非対応 非対応 非対応 非対応 対応

DOMHTMLImplementation.createHTMLDocument(title)

引数 真の値 を指定する 偽の値 を指定する
第一引数 (title)

"new document" を指定する。

  • title要素 直下にテキストノードが生成される。
    生成されたテキストノードのdataプロパティ値は "new document" となる。

"Hello, World!" を指定する。

  • title要素 直下にテキストノードが生成される。
    生成されたテキストノードのdataプロパティ値は "Hello, World!" となる。

null を指定する。

  • title要素 直下にテキストノードが生成される。
    生成されたテキストノードのdataプロパティ値は null となる。

false を指定する。

  • title要素 直下にテキストノードが生成される。
    生成されたテキストノードのdataプロパティ値は false となる。

DOMImplementation.createDocument(namespaceURI, qualifiedName, doctype)

引数 真の値 を指定する 偽の値 を指定する
第一引数 (namespaceURI)

"http://www.w3.org/1999/xhtml" を指定する。

  • document[object Document] を返す。
  • document.documentElement[object HTMLHtmlElement] を返す。
  • document.body[object HTMLBodyElement] を返す。
  • document.doctype.namespaceURInull を返す。
  • HTMLDocument.innerHTML を使用できる。
    (HTMLBodyElement にメソッドが継承される)

null を指定する。

  • document[object Document] を返す。
  • document.documentElement[object Element] を返す。
  • document.bodynull を返す。
  • HTMLDocument.innerHTML を使用できない。
    ([object Element] は HTMLDocument と関わりがないので継承できない)
第二引数 (qualifiedName)

"html" を指定する。

  • document.documentElement.tagName"html" を返す。

"hoge" を指定する。

  • document.documentElement.tagName"hoge" を返す。

null を指定する。

  • document.documentElementnull を返す。
第三引数 (doctype)

[object DocumentType] を指定する。

  • document.doctype[object DocumentType] を返す。
  • document.doctype.publicIdcreateDocumentType() の 第二引数(publicId) を返す。
  • document.doctype.systemIdcreateDocumentType() の 第三引数(systemId) を返す。
  • document.doctype.namespaceURInull を返す。

null を指定する。

  • document.doctypenull を返す。

DOMImplementationLS.createLSParser(mode, schemaType)

DOMParser()

ActiveXObject("htmlfile")

備考

独り言

LSParser の使い方は自信を持って自信がないと言い切れます。(えへん)

[object XMLDocument] って何なんだ…。

[object XMLDocument] って何なんだ…。

DOMParser() は Firefox の独自拡張だけど、癖がなくて使いやすい。反面、DOM L3標準のcreateDocument() はブラウザの実装が追いついていない感じ。Firefox, Opera の挙動が特におかしい。

参考URL

DOM仕様書
XML仕様書
HTML5
MSDN
その他