用JavaScript实现asp中的UrlEncode和UrlDecode功能

 
<title>用JavaScript实现asp中的UrlEncode和UrlDecode功能 </title>
<script language="vbscript"> 
Function str2asc(strstr) 
 str2asc = hex(asc(strstr)) 
End Function 
Function asc2str(ascasc) 
 asc2str = chr(ascasc) 
End Function 
</script> 

<script language="javascript"> 
/*这里开始时UrlEncode和UrlDecode函数*/ 
function UrlEncode(str){ 
  var ret=""; 
  var strSpecial="!\"#$%&'()*+,/:;<=>?[]^`{|}~%"; 
  for(var i=0;i<str.length;i++){ 
   var chr = str.charAt(i); 
    var c=str2asc(chr); 
    //tt+= chr+":"+c+"n"; 
    if(parseInt("0x"+c) > 0x7f){ 
      ret+="%"+c.slice(0,2)+"%"+c.slice(-2); 
    }else{ 
      if(chr==" ") 
        ret+="+"; 
      else if(strSpecial.indexOf(chr)!=-1) 
        ret+="%"+c.toString(16); 
      else 
        ret+=chr; 
    } 
  } 
  return ret; 
} 
function UrlDecode(str){ 
  var ret=""; 
  for(var i=0;i<str.length;i++){ 
   var chr = str.charAt(i); 
    if(chr == "+"){ 
      ret+=" "; 
    }else if(chr=="%"){ 
     var asc = str.substring(i+1,i+3); 
     if(parseInt("0x"+asc)>0x7f){ 
      ret+=asc2str(parseInt("0x"+asc+str.substring(i+4,i+6))); 
      i+=5; 
     }else{ 
      ret+=asc2str(parseInt("0x"+asc)); 
      i+=2; 
     } 
    }else{ 
      ret+= chr; 
    } 
  } 
  return ret; 
} 

function decodeURL(str) 
{
 
  var ss = decodeURI(str);
  return ss;
}

function encodeURL(str) 
{
 
  var ss = encodeURI(str);
  return ss;
}

 
aa=UrlEncode("三山街")
bb=UrlDecode("%C8%FD%C9%BD%BD%D6")
document.write("编码后:"+aa+"<br>");
document.write("解码后:"+bb);
</script>
<p><br>
    <textarea name="s1" cols="50" rows="5" id="s1"></textarea>
  <br>
  <br>
    <textarea name="s2" cols="50" rows="5" id="s2"></textarea>
  <br>
  <br>
  <input type="button" name="Submit" value="编码UrlEncode" onClick="s2.value=UrlEncode(s1.value)">
  <input type="button" name="Submit" value="解码UrlEncode" onClick="s1.value=UrlDecode(s2.value)">
</p>
<p><br>
  <textarea name="a1" cols="50" rows="5" id="a1"></textarea>
  <br>
  <br>
  <textarea name="a2" cols="50" rows="5" id="a2"></textarea>
</p>
<p>
  <input type="button" name="Submit" value="编码UrlEncode" onClick="a2.value=encodeURL(a1.value)">
  <input type="button" name="Submit" value="解码UrlEncode" onClick="a1.value=decodeURL(a2.value)">
</p>
<p>&nbsp;</p>

 

你可能感兴趣的:(JavaScript,C++,c,C#,asp)