0

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.

11
  • Those characters are not special. Commented Jan 18, 2014 at 22:22
  • 1
    Save all files as UTF8 without BOM, set the charset to UTF8 and make sure everything that could possibly have a character encoding is set to UTF8 Commented Jan 18, 2014 at 22:23
  • 1
    ^^^ proof Commented Jan 18, 2014 at 22:23
  • "Save all files as UTF8 without BOM..." There's no reason they have to not have a BOM. Commented Jan 18, 2014 at 22:24
  • @T.J.Crowder - Don't like the byte order mark much, and some editors still adds that crap. Generally there's an option to save as "without BOM", I was just implying to choose that one. My editor doesn't have issues like this, so it's been a long time since I've had to fiddle with that stuff Commented Jan 18, 2014 at 22:25

1 Answer 1

4

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!)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.