您现在的位置:龙卷风首页 ›› 网络编程 ›› 阅读文章

js版的UrlEncode

js UrlEncode

代码:

<script>

//JS版的Server.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;

}

</script>

<!-----------------------调用范例------------------------>

<input type="text" id="Test" value="123?ABC、一二三。" />

<button onclick="alert(document.getElementById('Test').value.UrlEncode());">查看Url编码</button>

<!-------------------------------------------------------->

来源:http://bbs.51js.com/thread-68915-1-1.html

本站注:这个编码方法似乎不止对中文编码,会连带把别的字符页进行编码,最终导致整个url不能用了。所以,为了达到目的,要对整条的url进行diy,对中文部分进行UrlEncode即可正常。

作者 不见不散 本文仅代表作者观点,与龙卷风资讯网立场无关。

我来说两句

内容/Content