Document.title
document.title
プロパティは、現在の文書の題名を取得または設定します。
構文
var docTitle = document.title;
docTitle は document の題名を含む文字列です。題名が document.title
に設定することで上書きされている場合は、その値を含みます。そうでなければ、マークアップの中で指定された題名を含みます (下記のメモを参照)。
document.title = newTitle;
newTitle
は文書の新しい題名です。代入すると、 document.title
の返値、文書に表示される題名 (ウィンドウのタイトルバーやタブなど)、文書の DOM (HTML 文書の <title>
要素の内容など) にも反映されます。
例
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<script>
alert(document.title); // "Hello World!" と表示
document.title = "Goodbye World!";
alert(document.title); // "Goodbye World!" と表示
</script>
</body>
</html>
注
Gecko では、このプロパティは HTML, SVG, XUL, その他の文書に適用されます。
HTML 文書では、 document.title
の初期値は <title>
要素のテキストの内容です。 XUL では、 <xul:window>
またはその他の最上位 XUL 要素の title
属性の値です。
XUL では、文書の読み込みが完了する前に document.title
にアクセスした時の動作は未定義です。 document.title
が空文字を返す場合があり、また document.title
を設定しても効果が表れないかもしれません。
仕様書
仕様書 | 状態 | 備考 |
---|---|---|
HTML Living Standard document.title の定義 |
現行の標準 |
ブラウザーの互換性
BCD tables only load in the browser