Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the Title of a Web Page Using Javascript [duplicate]

Tags:

javascript

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?

like image 630
user1060817 Avatar asked Dec 28 '11 19:12

user1060817


People also ask

How do you change the title of a website using JavaScript?

The textContent property of an element returns the text content of a specific node. The title of the page can be changed by assigning the required new title as a string to the textContent property. This will change the title of the website to the preferred title.

How do I change the title of a Web page?

Changing the Title Tag in your Website's Control Panel You would edit your title tags through the control panel you use for creating and editing website pages. Look for the section in which you can change meta tags; for more information, refer to your CMS provider's support.

How do I change the title of a JavaScript document?

Use the querySelector() Method to Change the Page Title in JavaScript. We can use the document. querySelector() method to pick elements in a document. The title element can be chosen by giving the title element a selector parameter and retrieving the page's main title element.


3 Answers

Use

document.title = 'Your desired title';
like image 160
Kristian Avatar answered Sep 21 '22 07:09

Kristian


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; }
like image 21
Odys Avatar answered Sep 18 '22 07:09

Odys


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).

like image 34
Dominic Green Avatar answered Sep 17 '22 07:09

Dominic Green