Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

unicode - international characters in Javascript

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 ???.

I know that in html, I use the "æøå" for ???.

however, as soon as the chars pass through JavaScript, I get black boxes with "?" in them when using ???, and "æøå" is printed as is.

I've made sure to set it to utf-8, but that isn't helping much.

Ideally, I want it to work with any special characters (naturally).

The example that isn't working is included below:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
        <script type="text/javascript" charset="utf-8">
            alert("&aelig;&oslash;&aring;");
            alert("???");

        </script>
    </head>
    <body>
    </body>
</html>

What am I doing wrong?


Ok, thanks to Grapefrukts answer, I got it working.

I actually needed it for data coming from an MySQL server. Since the saving of the files in UTF-8 encoding only solves the problem for static content, I figure I'd include the solution for strings from a MySQL server, pulled out using PHP:

utf8_encode($MyStringHere)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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("u00e6u00f8u00e5")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...