Change encoding from UTF-8 to ISO-8859-2 in Javascript

不羁岁月 提交于 2019-12-10 14:24:04

问题


I would like to change string encoding from UTF-8 to ISO-8859-2 in Javascript. How can I do it?

I need it because I've designed a widget. User just copies < script > tag from my site and puts it on his. This script creates div and puts into div widget contents with text.

If target website is in UTF-8 encoding - it works fine. But when it is in ISO-8859-2 than text that is encoded in UTF-8 is displayed on site with ISO-8859-2 and as a result I see trash.


回答1:


Instead of using e.g. "ĉ" in your JavaScript code, use Unicode escapes such as "\u0109".




回答2:


If you're in control of the output, you can replace all special characters with unicode escapes (e.g. \u00e4 for ä). The browser can interpret it correctly regardless of document encoding.

The easiest way to do this would be to put the string into a JSON encoder. Both PHP's and Ruby's does that. Don't know about other implementations though.

Another solution that might work is to add charset="utf-8" to the <script> tag.




回答3:


I suppose you just need to convert your wdiget from UTF-8 to ISO-8859-2 and provide 2 versions of script.



来源:https://stackoverflow.com/questions/2117024/change-encoding-from-utf-8-to-iso-8859-2-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!