日期:2014-05-16  浏览次数:20519 次

javaScript使用encodeURIComponent()转换URL编码
本人写的一个转换函数,用onClick事件替换<a href></a>,
function redirectURL(url,para) {
	window.location.href=url+encodeURIComponent(para);
	}

示例

<A class="pl" href="/product/product.asp?id=<%Response.Write(rs("PartNo"))%>">

替换成
<A class="pl" onClick="javascript:redirectURL('/product/product.asp?id=', '<%Response.Write(rs("PartNo"))%>')">


附录
在下面的例子中,我们将演示用encodeURIComponent()来编译URIs:
<script type="text/javascript">

document.write(encodeURIComponent("http://www.test.com"))
document.write("<br />")
document.write(encodeURIComponent("http://www.test.com/p 1/"))
document.write("<br />")
document.write(encodeURIComponent(",/?:@&=+$#"))

</script>

输出结果为:
http%3A%2F%2Fwww.test.com
http%3A%2F%2Fwww.test.com%2Fp%201%2F
%2C%2F%3F%3A%40%26%3D%2B%24%23