简体   繁体   English

Javascript字符编码

[英]Javascript character encoding

In an external javascript file I have a function that is used to append text to table cells (within the HTML doc that the javascript file is added to), text that can sometimes have Finnish characters (such as ä). 在外部javascript文件中,我有一个函数,用于将文本追加到表格单元格(在添加javascript文件的HTML文档中),有时可以包含芬兰字符的文本(例如ä)。 That text is passed as an argument to my function: 该文本作为参数传递给我的函数:

content += addTableField(XML, 'Käyttötarkoitus', 'purpose', 255);

The problem is that diacritics such as "ä" get converted to some other bogus characters, such as " ". 问题是像“ä”这样的变音符号会被转换为其他一些虚假的字符,例如“ ”。 I see this when viewing the HTML doc in a browser. 我在浏览器中查看HTML文档时看到了这一点。 This is obviously not desirable, and is quite strange as well since the character encoding for the HTML doc is UTF-8. 这显然是不可取的,并且非常奇怪,因为HTML文档的字符编码是UTF-8。

How can I solve this problem? 我怎么解决这个问题?

Thanks in advance for helping out! 在此先感谢您的帮助!

The file that contains content += addTableField(XML, 'Käyttötarkoitus', 'purpose', 255); 包含content += addTableField(XML, 'Käyttötarkoitus', 'purpose', 255);的文件content += addTableField(XML, 'Käyttötarkoitus', 'purpose', 255); is not saved in UTF-8 encoding. 未以UTF-8编码保存。

I don't know what editor you are using but you can find it in settings or in the save dialog. 我不知道您使用的编辑器,但您可以在设置或保存对话框中找到它。

Example: 例:

If you can't get this to work you could always write out the literal code points in javascript: 如果你不能让它工作,你总是可以在javascript中写出文字代码点:

content += addTableField(XML, 'K\u00E4ytt\u00f6tarkoitus', 'purpose', 255);

credit: triplee 信用:三重奏

To check out the character encoding announced by a server, you can use Firebug (in the Info menu, there's a command for viewing HTTP headers). 要查看服务器公布的字符编码,可以使用Firebug(在“信息”菜单中,有一个用于查看HTTP标头的命令)。 Alternatively, you can use online services like Web-Sniffer . 或者,您可以使用Web-Sniffer等在线服务。

If the headers for the external .js file specify a charset parameter, you need to use that encoding, unless you can change the relevant server settings (perhaps a .htaccess file). 如果外部.js文件的标头指定了charset参数,则需要使用该编码,除非您可以更改相关的服务器设置(可能是.htaccess文件)。

If they lack a charset parameter, you can specify the encoding in the script element, eg <script src="foo.js" charset="utf-8"> . 如果它们缺少charset参数,则可以在script元素中指定编码,例如<script src="foo.js" charset="utf-8">

The declared encoding should of course match the actual encoding, which you can normally select when you save a file (using “Save As” command if needed). 声明的编码当然应该与实际编码相匹配,您可以在保存文件时通常选择该编码(如果需要,使用“另存为”命令)。

The character encoding of the HTML file / doc does not matter any external ressource. HTML文件/ doc的字符编码与任何外部资源无关。

You will need to deliver the script file with UTF8 character encoding. 您需要使用UTF8字符编码提供脚本文件。 If it was saved as such, your server config is bogus. 如果它保存为这样,您的服务器配置是假的。

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

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