Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery .text() and unicode

I'd like to display the "Open Lock" character in my HTML link text.

If I do it directly it shows up correctly with <a id="myId">&#x1f512;</a>, but I found no way to change it dinamically with the jQuery .text() function, like in:

 $("#myID").text(openLockText); 

What should I put in openLockText?

like image 898
janesconference Avatar asked Jun 18 '12 16:06

janesconference


2 Answers

Javascript internally only supports UTF-16.

Because this is an extended 32-bit UTF character (not in the "Basic Multilingual Plane") you need to insert the "UTF-16 surrogate pair", which is helpfully provided on the same page that you linked to:

0xD83D 0xDD13 

i.e.

$('#myId').text('\ud83d\udd13'); 

More details can be found in RFC 4627, which is strictly speaking the format for JSON.

like image 145
Alnitak Avatar answered Sep 17 '22 13:09

Alnitak


editedIf it were a Unicode code point that could be represented in a single UTF-16 character, then ou could use JavaScript escape sequences in such situations:

$('#foo').text('\uXXXX'); 

However, because your character requires more bits, that doesn't work. It would probably be possible to construct the byte sequence that'd allow the character to be represented as UTF-16, but it'd be a pain. I'd go with .html().

Note that not all fonts provide glyphs for "exotic" code points like that, and in my experience those that do provide incredibly ugly ones.

like image 35
3 revs Avatar answered Sep 20 '22 13:09

3 revs