Showing posts with label Unicode. Show all posts
Showing posts with label Unicode. Show all posts

04 February 2016

Bookmarklet for Translating a Named Entity to a Unicode Value


In a previous post, we explained how to use Javascript to translate a named HTML entity to a Unicode value.

Here is the corresponding "Bookmarklet":

javascript:x=prompt("entity","");translateNamedEntity=function(a){var b=document.createElement("div");b.innerHTML=a;a=b.childNodes[0].nodeValue;return a.charCodeAt(0).toString(16)};x&&window.alert(translateNamedEntity(x));

To use it, simply create a new bookmark and enter the above code as the URL.

02 February 2016

Translating a named HTML entity to a Unicode value


Google Docs


We use Google for Work and Google Docs at Stand Sure almost exclusively.

One of our biggest headaches is inserting special characters.

For reasons unknown, the Google Docs "Special Characters" tool under "Insert" is sometimes flaky and results in a message that the page needs reloaded (this started happening to us when we switched to the "new" version of Google Docs).

Chrome OS


We use Chrome OS devices.


Unicode is really easy on Chrome OS


Ctrl+Shift+U followed by the hexadecimal number.

This is a really awesome feature.


The Headache


In most cases, we know from HTML experience that © results in a copyright symbol, ©, that § results in a section symbol, §, etc.

But remembering that the Unicode for © is

&#a9;

so that we can type Ctrl+Shift+Ua9 to get the appropriate symbol in Google Docs is nigh impossible.


The Solution


Javascript to the rescue


"use strict";

var translateNamedEntity = function translateNamedEntity(text, undefined) {
  var div = document.createElement("div");

  div.innerHTML = text;
  text = div.childNodes[0].nodeValue;
  return text.charCodeAt(0).toString(16);
}

Usage

Paste the code into a REPL console.

Type something like the following:

translateNamedEntity("©")

In this example, you will get back the following:

"a9"