Document.open() - ドキュメントを開く
open()は、Documentのメソッドです。ドキュメントを開き、関連メソッドで書き込みができる状態にします。このメソッドは3つの引数を指定すると、全く別の性質を持ち、Window.open()と同じ働きをします。
概要
- 名前
- open
- 所属
- Document
- IDL
[CEReactions] Document open(optional DOMString type = "text/html", optional DOMString replace = ""); WindowProxy open(USVString url, DOMString name, DOMString features);
- 仕様書
- https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-document-open
説明
第1引数(type)を省略、または"text/html"を指定した場合、HTML parserが関連付けられ、write()や、writeln()での書き込みに影響します。
第2引数(replace)に"replace"を指定した場合、履歴のエントリーに追加されず、現在のエントリーと置き換えられます。
3つの引数を指定すると、Window.open()と同じ働きをします。引数の詳細は該当ページの説明をご参考下さい。
デモ
Document.open()のデモです。2つの役割をそれぞれ、確認してみて下さい。
<!-- このコードは編集できます。 -->
<!DOCTYPE html>
<html>
<head>
<style>
div#result { white-space: pre-wrap ; }
</style>
</head>
<body>
<p><button id="run1">ドキュメントに書き込む</button></p>
<p><button id="run2">リンクを開く</button></p>
<hr>
<div id="result"></div>
<script>
document.getElementById( "run1" ).onclick = function () {
document.open() ;
document.write( "<b>Hello!!</b>" ) ;
document.close() ;
}
document.getElementById( "run2" ).onclick = function () {
document.open( "https://syncer.jp/", null, "" ) ;
}
</script>
</body>
</html>
サポート状況
Chrome | Firefox | Safari | Edge | IE | Opera | iOS Safari | Android |
---|---|---|---|---|---|---|---|
● | ● | ● | ● | ● | ● | ● | ● |
関連記事
- Document()
- Document()は、Documentのコンストラクタです。新しいオブジェクトを作成します。
- Window.moveBy()
- moveBy()は、Windowのメソッドです。相対位置を指定して、ウィンドウを移動します。
- URL
- URLは、URLアドレスの情報を管理するための機能を備えたインターフェイスです。
- Window.open()
- open()は、Windowのメソッドです。新しいウィンドウを開きます。