Vignesh Subramanian
Vignesh Subramanian

Reputation: 7289

character encoding in javascript file

I am default values of an object in a javascript file

var default = {
    Title : "Actualités",
    Channel : "French"
}

I am loading default values from here, but when I check the values in the console, the value for title is containing unknown character in place of "é". It is showing

Upvotes: 0

Views: 2075

Answers (3)

T.J. Crowder
T.J. Crowder

Reputation: 1075527

If you're talking about a web browser, the character set the browser uses to interpret the file is determined by the Content-Type header sent with the file by the server. (script tags also have a charset attribute, but if the server says something different, the server wins. Best to ensure the server is sending the right information.)

So the file must be written to storage using the character set that the server will tell the browser it's using. It's a fairly common error to store the file as Windows-1252 or ISO-8859-1, but have the server send it saying it's UTF-8, which causes the kind of issue you've raised.

Ensure that the encoding of the file and the encoding the server reports match, and characters won't get messed up.

Obligatory link to an article by one of the SO founders, Joel Spolsky: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!).

Upvotes: 3

UserStack
UserStack

Reputation: 104

You can use the encoded character for é \xE9 in your JS file. This should work irrespective of the encoding of the JS file. (If this is the only character that you would like to be addressed). Try the below code, it should solve your proble

var default = {
    Title : "Actualit\xE9s",
    Channel : "French"
}

Upvotes: 0

Hardipsinh Jadeja
Hardipsinh Jadeja

Reputation: 1297

Kindly add the following meta tag at the head element of html document :

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

The above code will force browser to load content as Unicode.

Upvotes: -1

Related Questions