javascript下的urlencode应用-函数

<script language=”vbscript”>
Function str2asc(strstr)
str2asc = hex(asc(strstr))
End Function
Function asc2str(ascasc)
asc2str = chr(ascasc)
End Function
</script>
<script language=”javascript” type=”text/javascript”>
function UrlEncode(str){
   var ret=””;
   var strSpecial=”!\”#$%&'()*+,/:;<=>?[]^`{|}~%”;
   for(var i=0;i<str.length;i++){
   var chr = str.charAt(i);
     var c=str2asc(chr);
     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;
}
</script>

 

用这样的办法调用:

<script language=”javascript” type=”text/javascript”>
alert(UrlEncode(“青春部落”));
</script>

javascript下的urlencode应用-函数

One comment

Leave a Reply