Home >Web Front-end >JS Tutorial >js dynamically modify browser title
title
is a special node element in html
. Because it can use document.getElementsByTagName("title")[0]
to get the content of the web page title
tag, but its value cannot be changed using document.getElementsByTagName("title")[0].innerHtml
. 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.
Through console.log(document.getElementsByTagName("title")[0])
, we found that <title># can be printed ## tag, there are only text nodes in the tag, so I guess it can only recognize
TextNode, so I set the value of title using
innerText method, and it succeeded.
After testing, the value of title can also be set through document.title.
console.log(document.title); # 可以获取title的值。 document.title = '需要设置的值'; # 设置title的值。For 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,
title has changed.
$('title').html('') $('title').text('')Both methods can be implemented in jq
In native js, we can dynamically modify thetitle
of the web page in two ways:
innerTextand
document.titleIn .
jq, we can modify it through
$('title').html('')or
$('title').text('').
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 cannot use
document.getElementsByTagName("title")[0].innerHtml to change it 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.
console.log(document.getElementsByTagName("title")[0]), we found that
TextNode
, so I set the value of title using innerText
method, and it succeeded. <pre class="brush:php;toolbar:false">document.getElementsByTagName("title")[0].innerText = '需要设置的值';</pre>
document.title method
After testing, the value of title can also be set through document.title. console.log(document.title); # 可以获取title的值。
document.title = '需要设置的值'; # 设置title的值。
For 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,
title has changed. jQuery method
$('title').html('') $('title').text('')
Both methods in jq can be implemented.
The above content is a tutorial on how to dynamically modify the browser title with js. I hope it can help everyone.
Related recommendations:
js for php development to modify the page css styleIntroduction to the use of js to modify the attributes of the prototype_javascript skillsHow to use js to modify the client registry_javascript skillsThe above is the detailed content of js dynamically modify browser title. For more information, please follow other related articles on the PHP Chinese website!