Javascript下的urlencode编码解码方法附decodeURIComponent

2019-06-06 09:53:51于丽

            else
            v=eval("&h"+ Mid(enStr,i+1,2) + Mid(enStr,i+4,2))
            deStr=deStr & chr(v)
            i=i+5
            end if
        else
            if c="+" then
            deStr=deStr&" "
            else
            deStr=deStr&c
            end if
        end if
    next
    URLDecode=deStr
End function
//-->
</script>

javascript(function UrlDecode())其实还是柔和使用了vbscript,好像在javascript环境中,对于asc、hex、chr相关的转换,如 str.charCodeAt(0).toString(16) 及 String.fromCharCode(str) 在不同编码下,对于中文的编码结果还不统一。
比如: vbscript str2asc/asc2str

<script type="text/vbscript">
Function str2asc(strstr)
str2asc = hex(asc(strstr))
End Function
Function asc2str(ascasc)
asc2str = chr(ascasc)
End Function
MsgBox str2asc("a")
MsgBox asc2str("&H61")'16进制转的61 转到 10进制就是 97
</script>


javascript str2asc/asc2str

<script type="text/javascript">
function str2asc(str){
return str.charCodeAt(0).toString(16);
}
function asc2str(str){
return String.fromCharCode(str);
}
alert(str2asc("a"));//
alert(asc2str("0x61"));//
</script>


演示:

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]