简体   繁体   English

javascript文件中的字符编码

[英]character encoding in javascript file

I am default values of an object in a javascript file 我是javascript文件中对象的默认值

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 "é" . 我正在从此处加载默认值,但是当我在控制台中检查值时,title的值包含未知字符代替"é" 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. 如果您正在谈论Web浏览器,则浏览器用来解释文件的字符集由服务器随文件发送的Content-Type标头确定。 ( 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.) script标签也具有charset属性,但是如果服务器说的内容有所不同,则服务器会获胜。最好确保服务器发送正确的信息。)

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. 将文件存储为Windows-1252或ISO-8859-1是一个相当常见的错误,但是服务器将其发送为UTF-8,这会引起您提出的问题。

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!) . SO创始人之一Joel Spolsky的文章强制链接: 绝对绝对是每个软件开发人员绝对肯定要了解的Unicode和字符集(无借口!)

You can use the encoded character for é \\xE9 in your JS file. 您可以在JS文件中将编码的字符用于é\\ xE9。 This should work irrespective of the encoding of the JS file. 无论JS文件的编码如何,它都应该起作用。 (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标签:

<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. 上面的代码将强制浏览器将内容加载为Unicode。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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