29

I would like to print a status message to my German users, which contains umlauts (ä/ü/ö). I also would like them be in the source file rather than having to download and parse some extra file just for the messages.

However, I can't seem to find a way to define the encoding of a JS source file. Is there something like HTML's http-equiv? Or should I define the encoding in the HTTP header?

When I simply encode the file in UTF-8 an serve it, IE displays garbage.

Leonard Ehrenfried
  • 1,573
  • 3
  • 20
  • 34

6 Answers6

42

Sending the encoding in the headers is always a good idea.

If that's not possible, the <script> tag has the charset property. W3C Reference

<script src="translations.js" type="text/javascript" charset="utf-8"/>
Kerem Baydoğan
  • 10,475
  • 1
  • 43
  • 50
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
10

It seems that the charset attribute for the script tag is not deprecated in HTML5 (it is deprecated only for a and link tags).

Simon Hi
  • 2,838
  • 1
  • 17
  • 17
3

While the charset parameter would seem helpful, it's important to note that it's actually deprecated in HTML5.

http://www.w3.org/International/questions/qa-html-encoding-declarations#charset

Guillaume Husta
  • 4,049
  • 33
  • 40
Micah Henning
  • 2,125
  • 1
  • 18
  • 26
  • 2
    So what's the alternative then? – Eloff Jan 19 '13 at 18:08
  • The alternative is to define the charset in the HTTP headers. – Micah Henning Jan 31 '13 at 21:24
  • 1
    @MicahHenning And what if you can't define it in the HTTP headers? – Timo Huovinen Oct 01 '13 at 10:36
  • Ultimately, it's the web server that defines the character set of the code being sent to the web browser, no matter if you use the `charset` parameter. – Micah Henning Oct 28 '13 at 19:13
  • 7
    Or what if you aren't using a web server and running HTML files locally? – Natalie Adams May 09 '14 at 23:45
  • 7
    HTML5 does not deprecated the property `charset` on a script tag. The link above only deals with links and not script. http://www.w3.org/TR/html5/scripting-1.html#attr-script-charset – Vern Burton Oct 26 '15 at 21:29
  • 2
    @VernBurton Yes, you are correct. Perhaps in June of 2012 when I made this post my understanding was that the `charset` parameter was deprecated, but then it was later re-added. After all, HTML 5 wasn't finalized until late 2014. – Micah Henning Nov 10 '15 at 19:29
3

This might sound a bit dumb, but make sure you save your javascript file with the correct encoding in the editor you are using. I had a problem with the encoding of my javascript file, and the solution was as simple as this!

CptCook
  • 121
  • 3
2

For french javascript js, we've used another charset :

<script src="./js/MyScript.js" type="text/javascript" charset="iso-8859-1"></script>
pjumble
  • 16,880
  • 6
  • 43
  • 51
-1

The simplest solution to your problem would be to use unicode escape for special character in strings. This way, your code is not affected by the page that's hosting it.

A tool that might help you: http://www.webstein.net/tools/javascript-unicode

ra00l
  • 570
  • 4
  • 20