Home > Web Front-end > JS Tutorial > How to modify browser title with javascript

How to modify browser title with javascript

青灯夜游
Release: 2023-01-07 11:41:16
Original
5936 people have browsed it

How to modify the title using JavaScript: 1. Use the "document.getElementsByTagName("title")[0].innerText='value';" statement to modify it. 2. Use the "document.title='value';" statement to modify it.

How to modify browser title with javascript

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

title is a special node element in HTML. How to modify title using JavaScript? The following article will introduce you to the modification method.

Method 1: innerText method

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

Method 2: document.title method

After testing, it works. The value of title can be set through document.title.

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

Example

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

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 modify browser title with javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template