1

Is there any function in JavaScript that can give me conversion from an HTML entity (name) to either numeric, or hexadecimal, or even ISO slash character (to be used in CSS)?

E.g.,

  • The cent symbol's ( ¢ ) HTML is : ¢.
  • Its numeric value is ¢ which can be used in HTML mark-up as well
  • Hexadecimal value is %A2
  • ISO is \00a2 - this I can use directly in CSS content property.

Like as follows:

.cents:after {
    font-style: italic;
    content: "\00a2";
}

I am more interested in the ISO value therefore.

Om Shankar
  • 7,989
  • 4
  • 34
  • 54
  • Is this what you mean? http://stackoverflow.com/questions/5786483/char-to-hex-in-javascript – Pieter Jul 22 '13 at 08:04
  • You can use [this](http://stackoverflow.com/a/9609450/50079) to convert entities to strings; after you have a string it's trivial to get the character with [`charCodeAt(0)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt) and convert to any representation you like. – Jon Jul 22 '13 at 08:29
  • Why would you need such a function in JavaScript? – Jukka K. Korpela Jul 22 '13 at 12:23

2 Answers2

2

HTML entities are defined in HTML, not JavaScript. To use the entity names in JavaScript, you thus need to make a JavaScript interpreter parse data as HTML. A simple way to achieve this is to assign data to the innerHTML property of an element. So starting from an entity name, construct an HTML entity reference by prefixing it with & and suffixing it with ;, assign it, then process the (parsed) value, a character. You can convert the character to its Unicode number using charCodeAt() and then write the number in hexadecimal using toString() with 16 as the argument:

var ent = 'cent'; // just an example; "ent" contains the entity name
var elem = document.createElement('span');
elem.innerHTML = '&' + ent + ';';
var ref = '\\' + elem.innerHTML.charCodeAt(0).toString(16);
// now ref contains a string like \a2 (you may need to add zeros or append a
// space if you write this into CSS

I don’t see what the point is here, though. You could use just the characters themselves in HTML, JavaScript, and CSS, provided that you deal with character encoding issues properly, as you should anyway.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
  • Is there any way to input `¢` and get the output as `¢` without using a data structure with all codes – sabithpocker Jul 25 '13 at 22:21
  • @sabithpocker, that’s a different question and should be asked as a new question with a new title. I don’t see why you would want to such things, and I don’t think it’s possible, though. – Jukka K. Korpela Jul 26 '13 at 04:27
1

The cent symbol's ( ¢ ) HTML is : ¢.

//will need to work against a list of all items or the char code set you will need

Its numeric value is ¢ which can be used in HTML mark-up as well

'&#' + '¢'.charCodeAt(0);

Hexadecimal value is %A2

escape('¢');

ISO is \00a2 - this I can use directly in CSS content property.

var iso = ['\\', '0', '0', '0', '0'];
var hex = '¢'.charCodeAt(0).toString(16).split('');

for(var i=4; i >= 0 && hex.length > 0 ; i--){
    iso[i] = hex.pop();
}

iso.join('')
sabithpocker
  • 15,274
  • 1
  • 42
  • 75