How to set page title with JavaScript

青灯夜游
Release: 2023-01-07 11:46:06
Original
9465 people have browsed it

Method to set the page title: 1. Use the statement "document.getElementsByTagName("title")[0].innerText='The value that needs to be set';"; 2. Use the statement "document.title = 'Required Set the value of the ';' statement.

How to set page 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

because it can be obtained usingdocument.getElementsByTagName("title")[0]The title tag of the web page, but its value cannot be changed usingdocument.getElementsByTagName("title")[0].innerHtml.

After testing, there are two ways to modify the native js, which are introduced below.

JavaScript method of setting page title

1. InnerText method

Through console.log (document.getElementsByTagName("title")[0]), I found that thelabel 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. </p>

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

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的值。
Copy after login

Example

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

[Recommended learning:javascript advanced tutorial]

The above is the detailed content of How to set page 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!