使用 Javascript 更改网页的标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8660456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:57:12  来源:igfitidea点击:

Changing the Title of a Web Page Using Javascript

javascript

提问by user1060817

Possible Duplicate:
How to dynamically change a web page's title?

可能的重复:
如何动态更改网页的标题?

I am trying to set the title of my web page using JavaScript because I want something that is stored in localStorage to be part of the title. The approach that I am trying is to use document.createElement('title'), but I cannot set the text of the title when I do that. Is there a way to set the text of the title this way or is there a different way that I should be doing this?

我正在尝试使用 JavaScript 设置我的网页的标题,因为我希望存储在 localStorage 中的内容成为标题的一部分。我正在尝试的方法是使用 document.createElement('title'),但是当我这样做时我无法设置标题的文本。有没有办法以这种方式设置标题的文本,或者我应该这样做吗?

回答by Kristian

Use

document.title = 'Your desired title';

回答by Odys

Your site should have a <title>element If it does, you simply add this to your scripts and call it

你的网站应该有一个<title>元素如果有,你只需将它添加到你的脚本中并调用它

function changeTitle(title) { document.title = title; }

回答by Dominic Green

The question is why are you doing it?

问题是你为什么要这样做?

You can just do something like, document.title = "This is the new page title.";, but this would not work for seo etc, as most crawlers do not support JavaScript.

您可以执行类似 document.title = "This is the new page title."; 的操作,但这不适用于 seo 等,因为大多数爬虫不支持 JavaScript。

If you want this to be compatible with most of the important crawlers, you're going to need to actually change the title tag itself, on the server side which would involve (PHP, or the like).

如果您希望它与大多数重要的爬虫兼容,您将需要在涉及(PHP 等)的服务器端实际更改标题标签本身。