-->

Javascript shows non-arabic characters when retrie

2019-06-09 20:50发布

问题:

I used an external page to get information from the user,

Picture:Getting info from the user

This information contains Arabic and English actually.

I use this code for that :

 
                    <form type=get action="http://ershadchem.yoo7.com/post?f=16&mode=newtopic">
  <table>
    <tr>
    <td>First Name:</td>
    <td><input type=text name=firstname size=10></td>
    </tr>
    <tr>
    <td>Last Name:</td>
    <td><input type=text name=lastname size=10></td>
    </tr>
    <tr>
    <td>Age:</td>
    <td><input type=text name=age size=10></td>
    </tr>
    <tr>
    <td colspan=2><input type=submit value="Submit">
    </td>
    </tr>
  </table>
      </form>

when the user enter information (may be Arabic, English or both), I use them in another page by a JavaScript code:

                                       
<script LANGUAGE="JavaScript">
                                                      
                                                      
  function getParams(){
  var idx = document.URL.indexOf('?');
  var params = new Array();
  if (idx != -1) {
    var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
  for (var i=0; i<pairs.length; i++){
    nameVal = pairs[i].split('=');
    params[nameVal[0]] = nameVal[1];
    }
  }
  return params;
 }
params = getParams();
firstname = unescape(params["firstname"]);
lastname = unescape(params["lastname"]);
age = unescape(params["age"]);
document.write("firstname = " + firstname + "<br>");
document.write("lastname = " + lastname + "<br>");
document.write("age = " + age + "<br>");

var getenwanid = document.getElementById("enwanid");
var getenwanid0 = document.getElementById("enwanid0");



</script>

The result is perfect in English in the page :

Picture:English information is retrieved perfectly

But when user enters information in Arabic:

Picture:type info in Arabic

the result is non-arabic characters: Picture:Non-arabic characters are shown in picture

I hope I could get an edit for the codes that helps me show Arabic characters properly.

Thank You.

回答1:

unescape is broken. Use decodeURIComponent instead.