9

Possible Duplicate:
How to dynamically change a web page's title?

I am trying to set the title of my web page using JavaScript because I want something that is stored in localStorage to be part of the title. The approach that I am trying is to use document.createElement('title'), but I cannot set the text of the title when I do that. Is there a way to set the text of the title this way or is there a different way that I should be doing this?

Community
  • 1
  • 1
user1060817
  • 153
  • 1
  • 1
  • 3

3 Answers3

29

Use

document.title = 'Your desired title';
Tom van der Woerdt
  • 28,143
  • 7
  • 67
  • 105
Kristian
  • 2,858
  • 1
  • 19
  • 39
3

Your site should have a <title> element If it does, you simply add this to your scripts and call it

function changeTitle(title) { document.title = title; }
Odys
  • 8,319
  • 8
  • 63
  • 105
1

The question is why are you doing it?

You can just do something like, document.title = "This is the new page title.";, but this would not work for seo etc, as most crawlers do not support JavaScript.

If you want this to be compatible with most of the important crawlers, you're going to need to actually change the title tag itself, on the server side which would involve (PHP, or the like).

Incognito
  • 19,550
  • 15
  • 73
  • 117
Dominic Green
  • 9,716
  • 4
  • 27
  • 34