While character encoding and character sets are pretty easy to understand, it used to be pretty tough back in the old days. Luckily modern browsers handle encoding pretty well, and we can even state that when you are using UTF-8 in your document and in your database, nothing can go wrong really.

But the shit really hits the fan when you are using a third party application that does not use UTF-8 in its database but you need to use the content from that database in an HTML file that does use UTF-8 or when that party rewrites stuff on the fly. That’s what happened to us recently when we were working with a HTML creative in the console of the adserver Appnexus.

In our creative HTML that we uploaded to Appnexus we had the literal "€" sign.

var price = '€ 5.00';

This should not cause any problems using UTF-8, but it did. It rendered a weird ‘a’ character, indicating the character is not available in the current character set for the set encoding type.

So we tried to change it to the HTML entity of the Euro sign.

var price = '€ 5.00';

Unfortunately this got rendered as €. This sucks. It looks like Appnexus translated this to € or something. Same thing with the numeric reference €.

This was driving us crazy and felt like we’ve had already spent far too many time on this weird specific case.

String.fromCharCode to the rescue

This is when we got our hands dirty and reached for our friend from the past: fromCharCode. String.fromCharCode is a static method of the String object and converts a Unicode number into a character. Using this method we can get round Appnexus rewriting magic.

First we need to find out what the Unicode number is for the Euro sign. Let’s open our console and write:

  // 8364

Ok, so now we know the Unicode number for the Euro sign is 8364. Cool. Now in our creative, let’s replace the literal "€" with the fromCharCode method call.

var euroSign = String.fromCharCode(8364);
  var price = euroSign + ' 5.00';

That’s it, the Euro sign now renders correctly in our HTML output. We totally tricked you there, Appnexus console!

Of course this is only an edge case, and Appnexus will come with a better solution for this. But still I think it’s a neat way to go round crazy encoding problems when you don’t have total control over the environment.

Leave a Reply