Home  >  Article  >  Web Front-end  >  How to change the title title with javascript

How to change the title title with javascript

青灯夜游
青灯夜游Original
2021-06-11 17:49:172380browse

Change method: 1. Use the "document.getElementsByTagName("title")[0].innerText="value that needs to be set";" statement to modify; 2. Use "document.title="Need to set The value of ";" statement to modify.

How to change the title title with javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript to change the title title

1. innerText method

through console. log(document.getElementsByTagName("title")[0]), I found that the b2386ffb911b14667cb8f0f91ea547a7 label can be printed. There are only text nodes in the label, so I guess it can only recognize TextNode, so I used innerText to set the value of title, and it succeeded. .

document.getElementsByTagName("title")[0].innerText = '需要设置的值';

2. document.title method

After testing, the value of title can also be set through document.title.

console.log(document.title);      # 可以获取title的值。
document.title = '需要设置的值';    # 设置title的值。

Example

window.onfocus = function () {
 document.title = '恢复正常了...';
};
window.onblur = function () {
 document.title = '快回来~页面崩溃了';
};

We change the value of title when the browser gains focus and loses focus. You can find that the title changes when switching browser tabs.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of How to change the title title with javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn