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 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
精彩评论