JS版的Server.UrlEncode

时间:2023-03-09 22:02:53
JS版的Server.UrlEncode

<script>
function (str) {//标准UrlEncode
.execScript("function reHex(str)\
reHex=hex(asc(str))\
end function \r\n\
function jsChr(str)\
jsChr=Chr(str)\
end function", "vbscript")

? String.prototype.UrlEncode = function() {
glbEncode = {};
return escape(this).replace(/%u(.{4})/g,
function(a, b, c, d) {
return reHex(String.fromCharCode(eval("0x" + b))).replace(/(.{2})(.{2})/, "%$1%$2")
}).replace(/\+/g, "%2B").replace(/%20/g, "+");
}
return str.UrlEncode();
}

function urlEncode(str) {//JS版的.UrlEncode编码函数,所有字符全替换
String.prototype.urlEncode = function() {
var str = this;
str = str.replace(/./g,
function(sHex) {
window.EnCodeStr = "";
window.sHex = sHex;
window.execScript('window.EnCodeStr=Hex(Asc(window.sHex))', "vbscript");
return window.EnCodeStr.replace(/../g, "%$&");
});
return str;
}
return str.urlEncode();
}

.writeln(UrlEncode('~!@#$%^&*()_+<> 为什么?')+"<br>");
document.writeln(urlEncode('~!@#$%^&*()_+<> 为什么?')+"<br>");
</script>

上面两个有个是标准的,有个是所有字符都替换成UrlEncode。