Link to home
Start Free TrialLog in
Avatar of kverbist
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?
ASKER CERTIFIED SOLUTION
Avatar of dbritt
dbritt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kverbist
kverbist

ASKER

thanks, I'm almost there,   while this code indeed works for the ascii codes you gave me, it still doesnt display any arabic or chinese, korean or japanese characters in my alert box, any idea how to make this happen?
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?

     平雪迎骨水直

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  = "&#72;&#101;&#108;&#108;&#111;&#32;&#87;&#111;&#114;&#108;&#100;&#33;"; // English "Hello World!"
            var str2 = "&#x4ECA;&#x65E5;&#x306F;&#32;&#x30EF;&#x30FC;&#x30EB;&#x30C9;&#33;";   // Japanese "Hello World!"

            var txtArea = document.getElementById("output");

            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:runTest();">

      <br><br>

      <textarea id="text1" name="output" rows="10" cols="50"></textarea>
</body>
</html>
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 :)
sorry, I forgot to accept yesterday ;)