DocumentType: before() メソッド

The DocumentType.before() 一連の Node オブジェクトまたは文字列を、この DocumentType の親ノードの子リストの、この DocumentType の直前に挿入します。文字列は等価な Text ノードとして挿入されます。

構文

js
before(param1)
before(param1, param2)
before(param1, param2, /* … ,*/ paramN)

引数

param1, …, paramN

挿入する一連の Node オブジェクト、または文字列です。

返値

なし (undefined)。

例外

HierarchyRequestError DOMException

ノードが階層内の指定の位置に挿入できなかった場合に発生します。

条件付きコメントの追加

コメントノードは doctype 宣言の前でも有効ですが、 IE で互換モードになることを考えるとお勧めできません。条件付きコメントは IE で有効です。

js
let docType = document.implementation.createDocumentType("html", "", "");
let myDoc = document.implementation.createDocument("", "", docType);

docType.before(
  document.createComment("<!--[if !IE]> conditional comment <![endif]-->"),
);

myDoc.childNodes;
// NodeList [<!--[if !IE]> conditional comment <![endif]-->, <!DOCTYPE html>]

仕様書

Specification
DOM Standard
# ref-for-dom-childnode-before①

ブラウザーの互換性

BCD tables only load in the browser

関連情報