kverbist
asked on
Javascript alert ASCII codes
I have built a website which is viewable in 12 different languages as it reads all the values for a specific language out of the MySql database.
Recently, my customer added Arabic, Chinese, Japanese and Korean language support. Everything works fine except for the alert boxes when someone forgets to fill in a certain text field when he/she registers.
MySql converts all foreign languages to ASCII codes so all those language values are in the form of 8 etc etc etc
When Javascript gives an alert it simply alerts exactly that, and not the actual characters themselves. Does anyone know how to solve this?
Recently, my customer added Arabic, Chinese, Japanese and Korean language support. Everything works fine except for the alert boxes when someone forgets to fill in a certain text field when he/she registers.
MySql converts all foreign languages to ASCII codes so all those language values are in the form of 8 etc etc etc
When Javascript gives an alert it simply alerts exactly that, and not the actual characters themselves. Does anyone know how to solve this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The characters are most likely stored in Unicode, not ASCII. Are they stored in Hexidecimal or Decimal?
Like, do they have an 'x' in front?
平雪迎&# x9AA8; C34;ݯ 4;
There's got to be some special way these values are being stored.
Like, do they have an 'x' in front?
平雪迎&#
There's got to be some special way these values are being stored.
I'm guessing that's the problem, because if it were all in decimal, it would work ;)
Here's a version that should work with the hex values just fine:
========================== ========== ========== ======
<html>
<head>
<title>Test Page</title>
<script language="JavaScript" type="text/javascript">
function runTest()
{
var str = "Hell&# 111; & #87;o r
 8;d&# 33;"; // English "Hello World!"
var str2 = "今日は& #32;̎ F;ー ル&# x30C9;! ;"; // Japanese "Hello World!"
var txtArea = document.getElementById("o utput");
txtArea.value = fromCharCodes(str);
txtArea.value += "\n\n";
txtArea.value += fromCharCodes(str2);
}
function fromCharCodes(str)
{
var answer = "";
var codeArray = str.replace(/&#/g, "").split(";");
var code;
for(var i = 0; i < codeArray.length; i++)
{
code = new Number("0" + codeArray[i]);
if(!isNaN(code.valueOf()))
{
answer += String.fromCharCode(code);
}
}
return answer;
}
</script>
</head>
<body>
<input type="button" value="Run Test" onclick="javascript:runTes t();">
<br><br>
<textarea id="text1" name="output" rows="10" cols="50"></textarea>
</body>
</html>
Here's a version that should work with the hex values just fine:
==========================
<html>
<head>
<title>Test Page</title>
<script language="JavaScript" type="text/javascript">
function runTest()
{
var str = "Hell&#
var str2 = "今日は&
var txtArea = document.getElementById("o
txtArea.value = fromCharCodes(str);
txtArea.value += "\n\n";
txtArea.value += fromCharCodes(str2);
}
function fromCharCodes(str)
{
var answer = "";
var codeArray = str.replace(/&#/g, "").split(";");
var code;
for(var i = 0; i < codeArray.length; i++)
{
code = new Number("0" + codeArray[i]);
if(!isNaN(code.valueOf()))
{
answer += String.fromCharCode(code);
}
}
return answer;
}
</script>
</head>
<body>
<input type="button" value="Run Test" onclick="javascript:runTes
<br><br>
<textarea id="text1" name="output" rows="10" cols="50"></textarea>
</body>
</html>
ASKER
hi dbritt, thank you very much, your first answer already was the right one, but because of a little mistake in my PHP code it didnt put the ASCII chars in there properly.
Good good! Don't forget to Accept the A+ code :)
ASKER
sorry, I forgot to accept yesterday ;)
ASKER