JavaScriptで新しいwindowを開き、文字を表示する方法/document.open・writeメソッドの使い方

elearning IT

windowオブジェクトのdocumentに文字を表示するdocumentメソッドのご紹介です。そのまま使うとページがまるごと更新されてしまうので、新しいウィンドウを作って実行しています。

スポンサーリンク

document.writeの使い方

新しくWindowを作る
window.open(URL,windowの名前,スタイル)
※windowは省略可能
documentに文字を入力
window.document.write(内容)
※windowは省略可能

とりあえず実装したものは、こんな感じになります。

//JavaScript
function windowWrite(){
var np = open("", "", "width=500,height=400");
np.document.write("画面を作りました");
np.document.write("<b><p>タグも挿入できます</p></b>");
}

ある意味ではdocumentというオブジェクトの範囲がよくわかるプログラムです。ちょっとしたマニュアルや連絡、簡単にブラウザで文字を表示することができるので、Console.logの他にテスト実行するのにも向いてます。

JavaScriptの使い方・メソッド・プロパティまとめ
HTML/CSSとセットでフロントエンドエンジニア三種の神器のひとつ、JavaScript系の記事まとめです。

コメント

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny

モバイルバージョンを終了