Richard
Richard

Reputation: 303

Set » HTML entity in JavaScript's document.title?

I'm setting document.title with JavaScript, and I can't find a way to supply » (&raquo) without it appearing as literal text.

Here's my code:

document.title = 'Home » site.com'; 

If I use &raquo ; in the title tag of the document it works great and displays correctly as », but it seems to be unescaping when I include it in document.title.

Any ideas?

thanks!

Upvotes: 13

Views: 11437

Answers (3)

htanata
htanata

Reputation: 36944

document.title takes the string as it is, so you can do this:

document.title = 'Home » site.com';

If you need to provide it as the entity name, you can set the innerHTML attribute. Here are two examples of how you can do it.

document.getElementsByTagName('title')[0].innerHTML = '»';
// or
document.querySelector('title').innerHTML = "»";

Upvotes: 10

Pointy
Pointy

Reputation: 413757

Try

document.title = 'Home \u00bb site.com';

Generally you can look up your special character at a site like this and then, once you know the numeric code, you can construct a Unicode escape sequence in your JavaScript string. Here, that character is code 187, which is "bb" in hex. JavaScript Unicode escapes look like "\u" followed by 4 hex digits.

Upvotes: 17

SLaks
SLaks

Reputation: 887479

Javascript does not use HTML entities.

You should simply use the actual » character in your string, and make sure that the file is saved and sent as UTF8.

Upvotes: 10

Related Questions