js dynamically modify browser title

小云云
Release: 2017-12-06 16:17:04
Original
1598 people have browsed it

titleis a special node element inhtml. Because it can usedocument.getElementsByTagName("title")[0]to get the content of the web pagetitletag, but its value cannot be changed usingdocument.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.

innerText method

Throughconsole.log(document.getElementsByTagName("title")[0]), we found that# can be printed ## tag, there are only text nodes in the tag, so I guess it can only recognize</code>TextNode<code>, so I set the value of title using</code>innerText<code>method, and it succeeded.</code></p><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">document.getElementsByTagName("title")[0].innerText = '需要设置的值';</pre><div class="contentsignin">Copy after login</div></div><div class="contentsignin">Copy after login</div></div>document.title method <h2></h2> <p>After testing, the value of title can also be set through<strong>document.title<code>.</code></strong></p> <div class="code" style="position:relative; padding:0px; margin:0px;"> <div class="code" style="position:relative; padding:0px; margin:0px;"> <pre class="brush:php;toolbar:false">console.log(document.title); # 可以获取title的值。 document.title = '需要设置的值'; # 设置title的值。</pre> <div class="contentsignin"> Copy after login </div> </div> <div class="contentsignin"> Copy after login </div> </div>For example: <p></p> <div class="code" style="position:relative; padding:0px; margin:0px;"> <div class="code" style="position:relative; padding:0px; margin:0px;"> <pre class="brush:php;toolbar:false">window.onfocus = function () { document.title = '恢复正常了...'; }; window.onblur = function () { document.title = '快回来~页面崩溃了'; };</pre> <div class="contentsignin"> Copy after login </div> </div> <div class="contentsignin"> Copy after login </div> </div>We change the value of title when the browser gains focus and loses focus. You can find that when switching browser tabs, <p>title<code>has changed.</code></p>jQuery method <h2></h2>Of course, if your project relies on jQuery, you can use the jq method to set it <p></p> <div class="code" style="position:relative; padding:0px; margin:0px;"> <div class="code" style="position:relative; padding:0px; margin:0px;"> <pre class="brush:php;toolbar:false">$('title').html('') $('title').text('')</pre> <div class="contentsignin"> Copy after login </div> </div> <div class="contentsignin"> Copy after login </div> </div>Both methods can be implemented in jq <p></p> <hr>Summary <h1></h1> <blockquote> In native js, we can dynamically modify the <p>title<code>of the web page in two ways:</code>innerText<code>and</code>document.title<code>In .</code>jq, we can modify it through<br>$('title').html('')<code>or</code>$('title').text('')<code>.</code></p> </blockquote> <p class="article fmt article__content"><br></p> <p>title<code>is a special node element in</code>html<code>. Because it can use</code>document.getElementsByTagName ("title")[0]<code>to get the</code>title<code>tag of the web page, but cannot use</code>document.getElementsByTagName("title")[0].innerHtml<code>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.</code></p>innerText method <h2></h2>Through <p>console.log(document.getElementsByTagName("title")[0])<code>, we found that</code><title># can be printed out ## tag, there are only text nodes in the tag, so I guess it can only recognizeTextNode, so I set the value of title usinginnerTextmethod, and it succeeded.

document.getElementsByTagName("title")[0].innerText = '需要设置的值';
Copy after login
Copy after login
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
Copy after login
For example:

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

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

Of course, if your project relies on jQuery, you can use the jq method to set it.

$('title').html('') $('title').text('')
Copy after login
Copy after login

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 style

Introduction to the use of js to modify the attributes of the prototype_javascript skills

How to use js to modify the client registry_javascript skills

The above is the detailed content of js dynamically modify browser title. 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!