Accents in alerts

§ DOM | JavaScript

Ever needed to put accented characters such as é into a JavaScript alert? It’s surprisingly problematic. Consider this simple function:

function accentTest1() {
alert('Ménage à trois.')

If you invoke the function, you get an alert as expected, but the HTML entities are not entified, they get them spelled out as typed in the code.

To get around this problem, you have to use octal-encoded characters instead of HTML entities:

alert('M351nage 340 trois.')

PJB has published a handy table for converting between octal, hex and HTML entities.