日期:2014-05-16  浏览次数:20485 次

关于编码的一段程序理解
function myurlencode(str){
  len = str.length;
  res = new String();
  charOrd = new Number();
   
  for (i = 0; i < len; i++) {
  charOrd = str.charCodeAt(i);
  if ((charOrd >= 65 && charOrd <= 90) || (charOrd >= 97 && charOrd <= 122) || (charOrd >= 48 && charOrd <= 57) || (charOrd == 33) || (charOrd == 36) || (charOrd == 95)) {
  // this is alphanumeric or $-_.+!*'(), which according to RFC1738 we don't escape
  res += str.charAt(i);
   
  }
  else {
  res += '%';
  if (charOrd > 255) 
  res += 'u';
  hexValStr = charOrd.toString(16);
  if ((hexValStr.length) % 2 == 1) 
  hexValStr = '0' + hexValStr;
  res += hexValStr;
  }
  }
   
  return res;
}

请问这段程序是要完成一个什么事的,再帮我稍微解答一下ELSE后面的一些语句。

------解决方案--------------------
我想是自定义的类似encodeURI 和encodeURIComponent 的程序

charOrd是i位置的Unicode 编码

res += '%'; 
这个懂吧
if (charOrd > 255)
res += 'u'; 
根据规则 字符值大于 255 的以 %uxxxx 格式存储 
hexValStr = charOrd.toString(16); 
返回charOrd的16进制形式
if ((hexValStr.length) % 2 == 1)
hexValStr = '0' + hexValStr; 
如果长度是单数就在前面加0
res += hexValStr; 
这个懂吧
------解决方案--------------------
(charOrd > = 65 && charOrd <= 90) ¦ ¦ (charOrd > = 97 && charOrd <= 122) 表示charOrd是大写或小写字母。