rodsarria
rodsarria

Reputation: 513

Special characters not displaying correctly in a Javascript string

I have a function that assigns a string containing specials characters into a variable, then passes that variable to a DOM element via innerHTML property, but it prints strange characters. Let's say I code this...

someText = "äêíøù";
document.getElementById("someElement").innerHTML = someText;

It prints the following text...

äêíøù

I know how to use the entity names to prevent this, but when I use them to pass the value through a Javascript method, they print literally.

Upvotes: 0

Views: 4288

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074295

This means that you have a conflict of encodings. Your JavaScript and your HTML are being served to the browser with different encodings/character sets. Ensure that they're encoded in and served with the same encoding / character set (UTF8 is a good choice) to make sure that characters are correctly interpreted.

Obligatory link: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

Upvotes: 4

Related Questions