admin管理员组文章数量:1356783
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 ä). 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. This is obviously not desirable, and is quite strange as well since the character encoding for the HTML doc is UTF-8.
How can I solve this problem?
Thanks in advance for helping out!
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 ä). 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. This is obviously not desirable, and is quite strange as well since the character encoding for the HTML doc is UTF-8.
How can I solve this problem?
Thanks in advance for helping out!
Share Improve this question asked May 30, 2012 at 16:06 Andrei OnigaAndrei Oniga 8,56917 gold badges54 silver badges91 bronze badges 9- 1 Are all your files (including the js one) served in UTF-8 ? Note that some servers (like some versions of tomcat) can break your js encoding. – Denys Séguret Commented May 30, 2012 at 16:07
- I don't know. And I don't know how to find out either... – Andrei Oniga Commented May 30, 2012 at 16:09
- 1 UTF-8 all the way through – TazGPL Commented May 30, 2012 at 16:11
- You can see it in Chrome inspector : network/your file/headers – Denys Séguret Commented May 30, 2012 at 16:12
- 1 No, because the editor may parse it for him, but not SAVE it the same way – Sebas Commented May 30, 2012 at 16:26
3 Answers
Reset to default 4The file that contains content += addTableField(XML, 'Käyttötarkoitus', 'purpose', 255);
is not saved in UTF-8 encoding.
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:
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 mand for viewing HTTP headers). Alternatively, you can use online services like 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).
If they lack a charset parameter, you can specify the encoding in the script
element, e.g. <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” mand if needed).
The character encoding of the HTML file / doc does not matter any external ressource.
You will need to deliver the script file with UTF8 character encoding. If it was saved as such, your server config is bogus.
本文标签: htmlJavascript character encodingStack Overflow
版权声明:本文标题:html - Javascript character encoding - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744029758a2578688.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论