Posted in Javascript onJuly 23, 2008
<script type="text/vbscript"> Function str2asc(strstr) str2asc = hex(asc(strstr)) End Function Function asc2str(ascasc) asc2str = chr(ascasc) End Function </script>
将vbscript函数转成javascript,方便非ie浏览器下使用
function str2asc(strstr){ return ("0"+strstr.charCodeAt(0).toString(16)).slice(-2); } function asc2str(ascasc){ return String.fromCharCode(ascasc); }
<script type="text/javascript"> /*这里开始时UrlEncode和UrlDecode函数*/ function UrlEncode(str){ var ret=""; var strSpecial="!\"#$%&'()*+,/:;<=>?[]^`{|}~%"; var tt= ""; 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; } alert(UrlDecode("%C2%D2%C2%EB")); </script>
用JavaScript实现UrlEncode和UrlDecode的脚本代码
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@