構文
 
  document.open() document.close()
総合 インデックス
メソッド
FirefoxIE

説明

  • 「open」メソッドは,新たにドキュメント・ストリームを開き,「close」メソッドは,ドキュメント・ストリームを閉じる。
  • 現在ブラウザに表示されているドキュメントを更新し,新たに「write」メソッドを使って,文字を書き出す場合は,まず「open」メソッドを使って,ドキュメント記述領域を新しく開き,そこに記述する。そしてドキュメントの記述が終了した時には,「close」メソッドを使って,記述の終了を指定する。実際には,「close」メソッドを記述しなくても,特に問題がない場合がほとんどである。しかし,ドキュメント記述の開始・終了を,明示的に指定するためにも,「open」メソッド,「close」メソッドは,セットで使用した方がいいだろう。

用例

ボタンフォームをクリックしたとき,新しいウインドウを開き,開いたウインドウにJavaScriptを使って文字列を書き出す。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta http-equiv="Content-Style-Type" content="text/css">
<title>open・close</title>
<script type="text/javascript">
<!--
function New_Window(){
    var NewWin;
    NewWin=window.open("","DocWin");
    NewWin.document.open();
    NewWin.document.write("<html><head><title>
New_Window</title></head>"); NewWin.document.write("<body>"); NewWin.document.write("<hr>"); NewWin.document.write("<p>"); NewWin.document.write("新しく開いたウインドウに"); NewWin.document.write("<br>"); NewWin.document.write("新たに文字を書き出しています"); NewWin.document.write("</p>"); NewWin.document.write("<hr>"); NewWin.document.write("</body></html>"); NewWin.document.close(); } //--> </script> <style type="text/css"> <!-- body { background-color: #ffffff; } --> </style> </head> <body> *新たに文字を記述する <p> <form> <input type="button" value="ウインドウを開く" onClick="New_Window()"> </form> </p> </body></html>

この用例を実行する

関連事項

ナビゲータ/window/open()