开发者

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

开发者 https://www.devze.com 2023-01-24 19:00 出处:网络
I have a HTML <title> element which I want to dynamically change depending on other elements. I tried using doc开发者_StackOverflow社区ument.getElementsByTagName(\'title\').innerHTML = dynamicCo

I have a HTML <title> element which I want to dynamically change depending on other elements. I tried using doc开发者_StackOverflow社区ument.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

0

精彩评论

暂无评论...
验证码 换一张
取 消