ドキュメント(Document)
- ドキュメントオブジェクト
- 色・背景色
- ドキュメントに書きこむ
- ドキュメント情報
- ドキュメントの要素
- その他
- document.cookie
- document.parentWindow
- document.expando
- document.embeds
- document.getSelection()
- document.selection
- document.selection.type
- document.selection.clear()
- document.selection.empty()
- document.selection.createRange()
- document.createStyleSheet([URL [, index]])
- document.showHelp(URL [, content])
- document.handleEvent(event)
- document.releaseEvents(events)
- document.routeEvent()
ドキュメントオブジェクト
window.document
表示している文書自体を示すドキュメントオブジェクトです。
色・背景色
document.bgColor
document.fgColor
document.linkColor
document.alinkColor
document.vlinkColor
<body> タグで指定する背景色(bgcolor)、テキストの色(text)、リンクテキストの色(link)、クリック中のリンクの色(alink)、参照済みのリンクの色(vlink)に対応する色を示す文字列です。値を代入することで、ダイナミックに色を変更することも可能です。
<form action="#"> <input type="button" value="red" onclick="document.bgColor='red'"> </form>
ドキュメントに書きこむ
document.open([mimeType [, replace]])
ドキュメントを開いて書込み可能にします。window.open() とは別物です。
top.main.document.open(); top.main.document.write("<html>\n"); top.main.document.write("<head><title>テスト</title></head>\n"); top.main.document.write("<body>へへへ</body>\n"); top.main.document.write("</html>\n"); top.main.document.close();
document.write(msg [, msg...])
document.writeln(msg [, msg...])
指定したドキュメントに文字列や値を書込みます。書き込みの最後は document.close() でクローズしないと、最後の行が表示されないことがあります。writeln() は write() とほぼ同様ですが、最後に改行コードが送り出される点が異なります。通常の HTML では改行は一文字のスペースになりますが、<pre>~</pre> などの間で用いると違いが分かります。現在では document.write() や document.writeln() を用いてドキュメントに書き込む方式はあまり推奨されていません。
document.writeln("<pre>"); document.writeln("AAA"); document.writeln("BBB"); document.writeln("</pre>");
document.close()
ドキュメントへの書き込みを閉じます。これを怠ると、write() や writeln() で書き出した内容がブラウザに表示されないことがあります。
document.clear()
ドキュメントの内容をすべてクリアします。
ドキュメント情報
document.lastModified
このドキュメントが最後に更新された時刻を示す文字列を返します。ブラウザやバージョンによって返される文字列のフォーマットはばらばらで、いろいろな問題があるため、使用するには注意が必要です。
Netscape Navigator 2.0 では「Sun Dec 31 23:59:59 2000」のような形式となり、これを Date() で解釈しようとするとアプリケーションエラーが発生することがあります。Netscape Navigator 3.0/4.0 では「12/31/00 23:59:59」のような形式となり、これを Date() で解釈しようとすると、00 を 1900 年と間違えてしまう問題があります。
IE3.0 では「日 12 31 23:59:59 2000」のような形式となり、これを Date() で解釈することはできません。IE4.0/IE5.0 では「12/31/2000 23:59:59」のような形式となりますが、IE4.0 では時間帯のバグがあり、タイムゾーン(TZ)の設定に関わらず常に UTC の値(9時間ずれた値)が返されます。
console.log("最終更新時刻: " + document.lastModified);
document.referrer
このページがどのページからジャンプしてきたか、ジャンプ元のページを示すアドレス(URL)を返します。セキュリティを考慮して参照できない場合もあります。
if (document.referrer) { console.log(document.referrer); }
document.URL
document.domain
ドキュメントのアドレス(URL)と、そのドメイン名部分を返します。
document.title
<title> で指定されたドキュメントのタイトルを示す文字列を返します。
document.charset
document.defaultCharset
このドキュメントのキャラクタセットを示す文字列を返します。シフトJIS の場合は "shift_jis"、EUC の場合は "euc-jp" となります。
document.readyState
ページのダウンロード状態を "uninitialized", "loading", "interactive", "complete" のいずれかの文字列で返します。
ドキュメントの要素
document.all
このドキュメント中のすべてのエレメント(要素)を参照します。→ エレメント。
document.activeElement
現在フォーカスが当たっているエレメントを返します。
document.elementFromPoint(x, y)
x, y 座標にあるエレメントを返します。
document.createElement(tag)
エレメントオブジェクトを新規に作成します。IE4.0 では OPTION と IMAGE のみ作成可能でしたが、IE5.0 ではすべての要素を作成可能になりました。作成したオブジェクトは、add() や appendChild() などで画面に反映させます。
<script> function func() { elm = document.createElement("OPTION"); elm.text = "CCC"; document.F1.S1.add(elm); } </script> <form name="F1" action="#"> <select name="S1" size=5> <option>AAA <option>BBB </select> <input type="button" onclick="func()" value="Add"> </form>
その他
document.cookie
「とほほのCookie入門」を参照。
document.parentWindow
親のウィンドウオブジェクト。
document.expando
オブジェクトの属性を拡張することが禁止されているかどうかを示す真偽値。初期値は true。例えば、属性の大文字と小文字を間違えて document.bgcolor = "red"; とした場合、自動的に bgcolor という新しい属性が追加されるが、document.expando = false; としておくことで、これを禁止することができるようになる。
document.embeds
<embed>に対応する埋め込みオブジェクトの配列。
document.getSelection()
現在選択されている文字列を得る。
document.selection
document.selection.type
document.selection.clear()
document.selection.empty()
document.selection.createRange()
現在選択されている文字列を示すオブジェクト。
xx = document.selection.createRange(); alert(xx.text);
document.createStyleSheet([URL [, index]])
このドキュメントに、別ファイルで記述したスタイルシートを適用する。URL には外部スタイルシートファイルのアドレス(URL)を、index には、styleSheets オブジェクトリスト上のインデックスを指定する。
<input type="button" value="夏のテーマ" onclick="document.createStyleSheet('summer.css')">
document.showHelp(URL [, content])
ヘルプを表示する。Microsoft HTML Help でのみ使用可能。
document.handleEvent(event)
document.releaseEvents(events)
document.routeEvent()
イベントに関する処理を行います。