简体   繁体   中英

How do you change the <title> element with javascript?

I have a HTML <title> element which I want to dynamically change depending on other elements. I tried using document.getElementsByTagName('title').innerHTML = dynamicContent but this did not seem to work. I have seen it done before, but I can't seem to figure out exactly how to do this.

Do you mean the <title> element in <head> of the page?
If yes, then changing document.title should do the trick.

getElementsByTagName() returns a NodeList, so you need to pick one element:

document.getElementsByTagName('title')[0].innerHTML = dynamicContent

There's also a shortcut to the title:

document.title = dynamicContent

You can manipulate

a) document.title = 'blah';

b) .textContent or .innerText depending on the browser

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM