Mar 29

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

felix021 @ 2008-3-29 02:13 [IT » 网络] 评论(0) , 引用(0) , 阅读(4268) | Via 本站原创 | |
@ 2009-04-30 补充:
其实Javascript里面有两个函数:encodeURI()  encodeURIComponent()

----

from http://hi.baidu.com/luoyeguigen/blog/item/01d632a8eee919b5cb130c95.html

用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;
}

ss="中国asp之家欢迎您!Aspxhome.com"
aa=UrlEncode(ss)
bb=UrlDecode(aa)
document.write("编码后"+aa+"<br>");
document.write("解码后:"+bb);
</script>




欢迎扫码关注:




转载请注明出自 ,如是转载文则注明原出处,谢谢:)
RSS订阅地址: https://www.felix021.com/blog/feed.php
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   *非必须
网址   电邮   [注册]