简体   繁体   中英

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

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

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"
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM