Main Page

Representation in JavaScript

could be represented by the same code, so the letter
in English could use the same code as a different let-
ter in a different language (obviously, not optimal).
Unicode represents characters as a 16-bit number, allowing for over 65,000 possible characters, making it an
ideal solution to internationalization concerns. Additionally, the first 128 Unicode characters are, in fact, the
128 ASCII characters, making compatibility with older English-language applications much easier.
Representation in JavaScript
All Unicode characters, including ASCII, are represented in Unicode as a four-digit hexadecimal value
prefixed with a
to indicate a Unicode character. For example,
is the Unicode form of the
(which can also be represented using ASCII syntax as
This representation of characters can be used in comments and strings in JavaScript just as you use spe-
cial characters like
. For example:
alert(“\u0048\u0045\u004C\u004C\u004F \u0057\u004F\u0052\u004C\u0044”);
Not sure what this line does? It presents an alert with the text
to the user. Using the
Unicode character set, you can create messages in any number of languages. Even though the plain text
form of such messages isn’t human readable, it’s still the only way to deal with multibyte characters
from other languages.
Browser versus operating system support
Just because JavaScript can display and understand Unicode characters doesn’t necessarily mean the
operating system can. Why should this concern Web developers who care only about what the browser
supports, you may ask? The answer is because JavaScript uses some operating system functionality to
do its job, although most developers never realize it. For internationalization, you must be aware of this
very important boundary.
Any time you use
, or
, you are using an operating system dialog box.
Unless the client operating system has foreign language support installed, you end up with a dialog full
of gibberish. Most of the time, the browser reflects the language of the operating system, however you
never can tell what individuals with do with their browsers.
When using operating system dialogs with internationalization, be aware that these problems can occur.
When dealing with a distributed Web application, it may be enough to inform the customer of this limi-
tation; on public Web sites, however, it may be best to avoid using these dialogs altogether.
Error-proofing strings
Oftentimes in internationalized Web pages, developers try to pass strings from a server-side variable
into a JavaScript variable using a technique such as this:
<% String sJspHello = “Hello”; %>
<!-- more code here -->
<script type=”text/javascript”>
var sJavaScriptHello = “<%= sJspHello %>”;
Deployment Issues
22_579088 ch19.qxd 3/28/05 11:43 AM Page 571

JavaScript EditorFree JavaScript Editor     Ajax Editor