Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript — escape a character entity (→ is showing as →)

document.title = ("hello → goodbye");

This is not outputting the arrow: "→" as it should. How does one escape that so it does?

like image 716
RGBK Avatar asked Apr 04 '11 13:04

RGBK


3 Answers

You don't need to escape it at all.

Just write

document.title = "hello → goodbye";

(and make sure your file is UTF8)

If you really want to escape it, you can use a Javsacript escape code: "\u2192"

Entities are only used in HTML source; you cannot use them in ordinary strings. (Except for innerHTML, which is HTML source)

like image 71
SLaks Avatar answered Oct 23 '22 19:10

SLaks


You need to use the JavaScript character escape sequence \u2192:

document.title = "hello \u2192 goodbye";

Or, as SLaks points out, if the JavaScript file is in Unicode, you can put it directly in the code:

document.title = ("hello → goodbye");
like image 7
David Fullerton Avatar answered Oct 23 '22 21:10

David Fullerton


I don't believe the HTML <title> tag permits mark-up at all, it just treats everything as a string literal. In other words, don't try to use HTML entities in your script, just use the actual "→" character.

like image 2
Ben Avatar answered Oct 23 '22 21:10

Ben