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

admin2025-03-19  4

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.

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.

Share Improve this question edited Jan 22, 2010 at 12:16 Tom Smykowski asked Jan 22, 2010 at 12:01 Tom SmykowskiTom Smykowski 26.1k57 gold badges165 silver badges247 bronze badges 6
  • Yes, 8859-2, it's character table for my Polish language – Tom Smykowski Commented Jan 22, 2010 at 12:07
  • 1 ... What for? Both JavaScript and most browsers, can handle UTF-8 just fine. – Ignacio Vazquez-Abrams Commented Jan 22, 2010 at 12:11
  • Are you wanting to change the encoding for the strings that JavaScript is using itself, or the encoding for the whole web page? – Mike DeSimone Commented Jan 22, 2010 at 12:17
  • I've updated question to explain 'what for' part. – Tom Smykowski Commented Jan 22, 2010 at 12:17
  • @Mike DeSimone Only text. I consider that it is not possible to change encoding of a div that i create from my script – Tom Smykowski Commented Jan 22, 2010 at 12:18
 |  Show 1 more ment

3 Answers 3

Reset to default 2

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

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.

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1742387270a211046.html

最新回复(0)