0

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

Vignesh Subramanian
  • 7,161
  • 14
  • 87
  • 150
  • 1
    In a browser? Or Node? Or...? (It matters, you see.) – T.J. Crowder Jan 08 '16 at 09:46
  • What encoding is the file saved in? Is it being served with any incorrect HTTP headers? – deceze Jan 08 '16 at 09:47
  • in a browser. I am getting values from this and assign value of a text box – Vignesh Subramanian Jan 08 '16 at 09:47
  • Don't forget to set Encoding to UTF 8 http://stackoverflow.com/questions/6790593/utf-8-and-javascript –  Jan 08 '16 at 09:48
  • 1
    Does your editor save as UTF-8 and your webserver serve as UTF-8? – Johannes Jander Jan 08 '16 at 09:48
  • @deceze its just a .js file. I am loading values from it – Vignesh Subramanian Jan 08 '16 at 09:48
  • 1
    Not what I'm talking about with "encoding". Maybe get a primer here: [What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text](http://kunststube.net/encoding/) – deceze Jan 08 '16 at 09:50
  • i think the encoding of the files must be set to UTF8. The problem is not at the level of HTML or or Content-Type headers, but instead a very basic issue of how your text file is saved to disk. More info - http://stackoverflow.com/questions/9853463/enable-utf-8-encoding-for-javascript – Pradeep Potnuru Jan 08 '16 at 09:53

3 Answers3

3

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

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    @Maurize: Oh, I don't care about rep. (I used to, some years ago, but I got over it.) I'm probably already maxed for the day anyway. I really like helping people. – T.J. Crowder Jan 08 '16 at 09:51
  • 1
    [Someone actually downvoted this answer because it perfectly answers a question...!?](http://i3.kym-cdn.com/photos/images/facebook/000/126/314/3cd8a33a.png) – deceze Jan 08 '16 at 09:51
  • I don't downvote your answer. I just think some questions don't need an advanced answer - The comments above should say enough. –  Jan 08 '16 at 09:56
0

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"
}
Vignesh Subramanian
  • 7,161
  • 14
  • 87
  • 150
UserStack
  • 104
  • 1
  • 4
-1

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.

Hardipsinh Jadeja
  • 1,180
  • 1
  • 13
  • 30