International Language Support in JavaScript

JavaScript is built to support a wide variety of world languages and their characters – from the old US ASCII up to the rapidly spreading UTF-8. This page clears up some of the difficulties encountered when dealing with multiple languages and their related characters.

JavaScript and Character Sets

When working with non-European character sets ("charsets"), you may need to make changes to the way your page references external JavaScript(.js) files. Ideally, your .js files should saved in the UTF-8 character set in order to maximize its multilingual features — though you can use a different charset that supports your language, at the potential expense of users who can't support it. Once your files are saved as UTF-8, they must be "served" in the UTF-8 charset in order to display correctly. There are a few ways to ensure this:

Serve the Web Page as UTF-8

If your page is already served as UTF-8 (i.e. Content-type=text/html; charset=UTF-8), you don't need to make any changes — all embedded files in an HTML document are served in the same charset as the document, unless explicitly specified not to by you. You can do this by:

  • Use the Content-type meta tag — place at the TOP of your page's <head> section.

    <meta name="http-equiv" content="Content-type: text/html; charset=UTF-8"/>)
  • Edit your webserver configuration to serve all documents as UTF-8
  • Send the Content-type header via your server-side scripts (i.e. PHP, ASP, JSP)

Use the charset attribute of the <script> tag

The easiest way to ensure your script is served as UTF-8 is to add a charset attribute (charset="utf-8") to your <script> tags in the parent page:

<script type="text/javascript" src="[path]/myscript.js" charset="utf-8"></script>

Modify your .htaccess files (Apache Only)

You can also configure your webserver to serve all .js files in the UTF-8 charset, or only .js files in a single directory. You can do the latter (in Apache) by adding this line to the .htaccess file in the directory where your scripts are stored:

AddCharset utf-8 .js