Dragonboy
Dragonboy

Reputation: 1

$('title').text() doesn't work in IE

I want to change the page title using jQuery, But it doesn't work in IE, Why?

The code like this:

alert( $('title').text() ); // empty in IE
$('title').text('Some Text!'); // Don't work in IE

Yes, we can use document.title = 'xxxx'; to change the title conent.

Upvotes: 0

Views: 504

Answers (5)

Ja͢ck
Ja͢ck

Reputation: 173562

alert($('title').text()); // empty in IE <= 8

The reason why this happens is because the page title is somewhat special in Internet Explorer <= 8; how special?

document.getElementsByTagName('title')[0].firstChild; // null

When you use jQuery.text() it iterates over all child nodes of an element and concatenates their text representations to form the final result; because the firstChild property is null, this will obviously not work.

The page title is available as innerText and innerHTML properties, but attempting to write those properties will cause an "unknown" runtime error.

Upvotes: 1

Anand
Anand

Reputation: 14915

$(document).prop('title', 'Some Text!');

Here to answer your original question. It's a known bug.

Upvotes: 1

Teemu
Teemu

Reputation: 23396

title.innerHTML is read-only in IE. Also looks like innerText has some limitations.

Upvotes: 1

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

if you want to use as much jQuery as you can,

$("title").innerHTML = $("title").attr("text", "New title");

works on IE8.

Using the document.title property is the simple cross-browser way to set or retrieve the document title. IE just doesn't want to play right

Upvotes: 0

shankri
shankri

Reputation: 11

try this

document.title = "This is new title.";

Upvotes: -1

Related Questions