international characters in Javascript

后端 未结 6 1303
有刺的猬
有刺的猬 2020-12-15 08:12

I am working on a web application, where I transfer data from the server to the browser in XML.

Since I\'m danish, I quickly run into problems with the characters

相关标签:
6条回答
  • 2020-12-15 08:42

    You can also use String.fromCharCode() to output a character from a numeric entity.

    e.g. String.fromCharCode( 8226 ) will create a bullet character.

    0 讨论(0)
  • 2020-12-15 08:44

    I use the code like this with Thai language. It's fine.

    $message is my PHP variable.

    echo("<html><head><meta charset='utf-8'></head><body><script type='text/javascript'>alert('" . $message . "');</script></body></html>");
    

    Hope this can help. Thank you.

    (I cannot post image of what I did as the system said "I don't have enough reputation", so I leave the image, here. http://goo.gl/9P3DtI Sorry for inconvenience.)

    Sorry for my weak English.

    0 讨论(0)
  • 2020-12-15 08:55

    If you ever can't set the response encoding, you can use \u escape sequence in the JavaScript string literal to display these characters.

    alert("\u00e6\u00f8\u00e5")
    
    0 讨论(0)
  • 2020-12-15 08:55

    I get "&aelig;&oslash;&aring;" for the first one and some junk characters for the next. Could it be that the javascript is not mangling (or mojibake) your letters but the alert dialog uses the system default font, and the font is incapable of displaying the letters?

    0 讨论(0)
  • 2020-12-15 08:59

    Just specifying UTF-8 in the header is not enough. I'd bet you haven't saved your file as UTF-8. Any reasonably advanced text editor will have this option. Try that and I'm sure it'll work!

    0 讨论(0)
  • 2020-12-15 09:01

    This works as expected for me:

    alert("&aelig;&oslash;&aring;");
    

    ... creates an alert containing the string "&aelig;&oslash;&aring;" whereas

    alert("æøå");
    

    ... creates an alert with the non-ascii characters.

    Javascript is pretty utf-8 clean and doesn't tend to put obstacles in your way.

    Maybe you're putting this on a web server that serves it as ISO-8859-1? If you use Apache, in your Apache config file (or in .httaccess, if you can override), you should have a line

    AddCharset utf-8 .js
    

    (Note: edited to escape the ampersands... otherwise it didn't make sense.)

    0 讨论(0)
提交回复
热议问题