Defining the character encoding of a JavaScript source file

32,410

Solution 1

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

Solution 2

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

Solution 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!

Solution 4

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

Solution 5

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

<script src="./js/MyScript.js" type="text/javascript" charset="iso-8859-1"></script>
Share:
32,410
Leonard Ehrenfried
Author by

Leonard Ehrenfried

Updated on August 26, 2020

Comments

  • Leonard Ehrenfried
    Leonard Ehrenfried over 3 years

    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.