因为要在地址栏里传中文参数,但是地址栏传中文参数是很危险的一件事情,我就用urlencode()转了下。

但是本以为JS的 encodeURI()能解码,但是很可惜不行,于是我找到了可以给PHP urlencode()生成的编码所对应的JS解码。

  1. function urldecode(encodedString){
  2. var output = encodedString;
  3. var binVal, thisString;
  4. var myregexp = /(%[^%]{2})/;
  5. function utf8to16(str)
  6. {
  7. var out, i, len, c;
  8. var char2, char3;
  9. out = "";
  10. len = str.length;
  11. i = 0;
  12. while(i < len)
  13. {
  14. c = str.charCodeAt(i++);
  15. switch(c >> 4)
  16. {
  17. case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  18. out += str.charAt(i-1);
  19. break;
  20. case 12: case 13:
  21. char2 = str.charCodeAt(i++);
  22. out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
  23. break;
  24. case 14:
  25. char2 = str.charCodeAt(i++);
  26. char3 = str.charCodeAt(i++);
  27. out += String.fromCharCode(((c & 0x0F) << 12) |
  28. ((char2 & 0x3F) << 6) |
  29. ((char3 & 0x3F) << 0));
  30. break;
  31. }
  32. }
  33. return out;
  34. }
  35. while((match = myregexp.exec(output)) != null
  36. && match.length > 1
  37. && match[1] != '')
  38. {
  39. binVal = parseInt(match[1].substr(1),16);
  40. thisString = String.fromCharCode(binVal);
  41. output = output.replace(match[1], thisString);
  42. }
  43. //output = utf8to16(output);
  44. output = output.replace(/\\+/g, " ");
  45. output = utf8to16(output);
  46. return output;
  47. }