Document.title - Web APIs 编辑

The document.title property gets or sets the current title of the document.

Syntax

var docTitle = document.title;

docTitle is a string containing the document's title. If the title was overridden by setting document.title, it contains that value. Otherwise, it contains the title specified in the markup (see the 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 or tab), and it also affects 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 title attribute of the <xul: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.

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of 'document.title' in that specification.
Living Standard

Browser compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:90 次

字数:3110

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文