首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

javaScript运用encodeURIComponent()转换URL编码

2012-11-09 
javaScript使用encodeURIComponent()转换URL编码本人写的一个转换函数,用onClick事件替换a href/a,fun

javaScript使用encodeURIComponent()转换URL编码
本人写的一个转换函数,用onClick事件替换<a href></a>,

function redirectURL(url,para) {window.location.href=url+encodeURIComponent(para);}

示例

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

替换成
<A 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.comhttp%3A%2F%2Fwww.test.com%2Fp%201%2F%2C%2F%3F%3A%40%26%3D%2B%24%23

热点排行
Bad Request.