查看效果:js實(shí)現(xiàn)url加密,解密
<!--代碼來自網(wǎng)絡(luò)-->
<title>js實(shí)現(xiàn)asp中的UrlEncode和UrlDecode--來自網(wǎng)絡(luò)</title>
<form action="" method="post" name="form">
<input type="text" size="50" name="code" maxlength="100" /><br />
<input type="submit" value="加密解密" />
</form>
<script language="vbscript">
Function str2asc(strstr)
str2asc = hex(asc(strstr))
End Function
Function asc2str(ascasc)
asc2str = chr(ascasc)
End Function
</script>
<script language="javascript">
/*這里開始時(shí)UrlEncode和UrlDecode函數(shù)*/
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=""
aa=UrlEncode(ss)
bb=UrlDecode(ss)
document.write("編碼后"+aa+"<br>");
document.write("解碼后:"+bb);
</script>
更多信息請(qǐng)查看IT技術(shù)專欄