Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript_howto thread: Special characters in alert messages.


Message #1 by pheal@q... on Thu, 13 Dec 2001 18:47:51
Hi

I've wanted to do this a couple of times, but never bothered to look very 
hard. However a quick browse through MSDN brought up the two methods you 
need (and they work for IE/NS!).

In order to convert a number to a Unicode character, which you want to do, 
you can use the fromCharCode method.
This works for a single character or a series of them.

For example
  alert("i is " + String.fromCharCode(105) + "!");
displays "i is i" in the alert, and
  alert(String.fromCharCode(83, 116, 114, 105, 110, 103) + "!");
will display "String!".

To go the other way, and convert Unicode to a number, use the charCodeAt 
method. The syntax for this is "my_string.charCodeAt(index)", where 
my_string is the string or String object, and inex is the position of the 
character.

For example:
  s = "fröhliche Weihnachten";
  alert(s.charCodeAt(2)); // the ö
displays "246".
This is equivalent to:
  alert("fröhliche Weihnachten".charCodeAt(2));

I hope this helps.

Merry Christmas

Phil

> I'm working on a web app to be used in German and English and I'm 
getting 
> weird characters for the special characters used in German. In the html 
> file I'm using the html entities like ü and ä to show the 
> characters in html, but they get that unknow block character in an 
english 
> browser or a question mark on a german os, and german browser. 
> 
> Any way I can display the real characters?
> 
> Thanks is advance.

  Return to Index