W3C.US

  • Increase font size
  • Default font size
  • Decrease font size
Home Tutorial Javascript Book Javascript's charset

Javascript's charset

E-mail Print PDF

The old version javascript may support limited charset, such as old US ASCII or Western European charset. But experienced so many years of development, javascript now can support a wide variety of world languages and their characters.

When you are working with non-European charsets, you may need to make changes to the way your page references external JavaScript(.js) files. Ideally, these .js files should saved in the UTF-8 charset, then they can be used in multi-languages environments.

 

And the <script> should add an explicit charset define like this:

<script type="text/javascript" src="/somescript.js" charset="utf-8"></script>

If not do this, the script file would be treated as the same charset as the charset define at HTML meta tag.

For example:

If the html charset meta tag is: 

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

 And the html file use an outer javascript file:

<script type="text/javascript" src="/myscript.js" ></script>

The myscript.js save in ANSI charset, its content:

var ttt="你好";

Now, if you use the variable ttt in the html file, for instance, call alert(ttt), it would show you a garbled alert.

To solve this problem, you can add charset="utf-8" to the script's tag, and re-save the myscript.js in utf-8 charset.

Show the variable:

 

 

  1. Save .js file in UTF-8 charset.
  2. Add charset="utf-8" to script tag, or use utf-8 as the page's charset.

 

 

Last Updated on Thursday, 30 April 2009 04:15