Home > Article > Web Front-end > Javascript modification browser title method example sharing
title is a special node element in HTML. Because it can use document.getElementsByTagName("title")[0] to get the title tag of the web page, but it cannot use document.getElementsByTagName("title")[0] .innerHtml is used to change its value. After testing, there are two ways to modify native js, and it can also be easily set in jQuery. Friends who are not sure can find out.
innerText method
Through console.log(document.getElementsByTagName("title")[0]), we found that the b2386ffb911b14667cb8f0f91ea547a7 label can be printed, and there are only text nodes in the label. , so I guessed that it could only recognize TextNode, so I used innerText to set the value of title, and it succeeded.
document.getElementsByTagName("title")[0].innerText = '需要设置的值';
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 when switching browser tabs, the title changes.
jQuery method
Of course, if your project relies on jQuery, you can use the jq method to set it
$('title').html('') $('title').text('')
jq Both methods can be achieved
Summary
In native js, we can dynamically modify the title of the web page through innerText and document.title.
Modify the browser's User-Agent in php to disguise your browser and operating system
JavaScript modification Browser tab title tips_javascript tips
Use JavaScript to modify the implementation code of the browser URL address bar_javascript tips
The above is the detailed content of Javascript modification browser title method example sharing. For more information, please follow other related articles on the PHP Chinese website!