Document.title

  • Revision slug: Web/API/document.title
  • Revision title: document.title
  • Revision id: 477173
  • Created:
  • Creator: kscarfone
  • Is current revision? No
  • Comment Updated tags

Revision Content

{{DomRef}}

Summary

Gets or sets the title of the document.

Syntax

var docTitle = document.title;

title is a string containing the document's title. If the title was overridden by setting document.title, returns that value. Otherwise returns the title specified in the markup (see the {{Anch("Notes")}} below).

document.title = newTitle;

newTitle is the new title of the document. The assignment affects the return value of document.title, the title displayed for the document (e.g. in the titlebar of the window), but does not affect the DOM of the document (e.g. the content of the <title> element in an HTML document).

Example

<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title> 
</head>
<body>

<script>
alert(document.title); // displays "Hello World!"
document.title = "Goodbye World!";
alert(document.title); // displays "Goodbye World!"
</script>

</body>
</html>

Notes

This property applies to HTML, SVG, XUL, and other documents in Gecko.

For HTML documents the initial value of document.title is the text content of the <title> element. For XUL it's the value of the {{XULAttr("title")}} attribute of the {{XULElem("window")}} or other top-level XUL element.

In XUL, accessing document.title before the document is fully loaded has undefined behavior (document.title may return an empty string and setting document.title may have no effect).

Specification

Revision Source

<div>
  {{DomRef}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>Gets or sets the title of the document.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<em>var docTitle</em> = <em>document</em>.title;
</pre>
<p><code>title</code> is a string containing the <code>document</code>'s title. If the title was overridden by setting <code>document.title</code>, returns that value. Otherwise returns the title specified in the markup (see the {{Anch("Notes")}} below).</p>
<pre class="syntaxbox">
<em>document</em>.title = <em>newTitle</em>;
</pre>
<p><code>newTitle</code> is the new title of the document. The assignment affects the return value of <code>document.title</code>, the title displayed for the document (e.g. in the titlebar of the window), but does not affect the DOM of the document (e.g. the content of the <code>&lt;title&gt;</code> element in an HTML document).</p>
<h2 id="Example" name="Example">Example</h2>
<pre class="brush:js">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Hello World!&lt;/title&gt; 
&lt;/head&gt;
&lt;body&gt;

&lt;script&gt;
alert(document.title); // displays "Hello World!"
document.title = "Goodbye World!";
alert(document.title); // displays "Goodbye World!"
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<h2 id="Notes" name="Notes">Notes</h2>
<p>This property applies to HTML, SVG, XUL, and other documents in Gecko.</p>
<p>For HTML documents the initial value of <code>document.title</code> is the text content of the <code>&lt;title&gt;</code> element. For XUL it's the value of the {{XULAttr("title")}} attribute of the {{XULElem("window")}} or other top-level XUL element.</p>
<p>In XUL, accessing <code>document.title</code> before the document is fully loaded has undefined behavior (<code>document.title</code> may return an empty string and setting <code>document.title</code> may have no effect).</p>
<h2 id="Specification" name="Specification">Specification</h2>
<ul>
  <li><a class="external" href="http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-18446827">DOM Level 2 HTML: document.title</a></li>
  <li><a class="external" href="http://www.whatwg.org/html/#document.title" title="http://www.whatwg.org/html/#document.title">HTML5</a></li>
</ul>
Revert to this revision