Joda
Joda

Reputation: 12886

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)

Upvotes: 15

Views: 47207

Answers (7)

andreszs
andreszs

Reputation: 2956

The alert() method won't decode UTF-8 encoded characters sent by AJAX requests, at least not when the webpage uses a non-UTF-8 encoding (such as ISO-8859-1).

Workaround (uses jQuery):

var msg = 'This is a bull &bull; and this is a star &#9733;';
var msgu = $('<span/>').html(msg).text();
alert(msgu);    

This works for all &xxx characters that the browser can display because the innerHTML() method (or html() in jQuery's case) creates an UTF-8 valid string.

Upvotes: 0

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.

Upvotes: 2

Kevin Hakanson
Kevin Hakanson

Reputation: 42200

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

Upvotes: 24

enricopulatzo
enricopulatzo

Reputation: 4479

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

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

Upvotes: 7

grapefrukt
grapefrukt

Reputation: 27045

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!

Upvotes: 12

Eugene Yokota
Eugene Yokota

Reputation: 95604

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?

Upvotes: 1

chryss
chryss

Reputation: 7519

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

Upvotes: 0

Related Questions