版权声明:本文为博主原创文章,未经博主容许不得转载。javascript
1传递参数的页面test01.htmhtml
<script type="text/javascript">
function send() {
var url = "test02.htm";
var userName = "这是谁";//假设参数值为这是谁java
window.open(encodeURI(url + "?userName=" + userName));浏览器
//encodeURI编码
}编码
</script>
<input id="btn" onclick="send()" value="点击" type="button" name="button"/>url
2接受并显示参数页面test02.htmspa
<div id="show"></div>
<script type="text/javascript">
var urlinfo = window.location.href; //获取url
var userName = urlinfo.split("?")[1].split("=")[1]; //拆分url获得“=”号后面的值(先用split("?")[1]获得?号之后的值,再用split("=")[1]获得等号后面的值,split从0开始计数)
document.getElementById("show").innerHTML = decodeURI(userName);//decodeURI解码firefox
</script>code
在浏览器中运行test01.htm 点击按钮,进入test02.htmhtm
ie中地址栏显示 http://localhost:17591/网页3-6纯html/test02.htm?userName=%E8%BF%99%E6%98%AF%E8%B0%81
firefox中地址栏显示:http://localhost:17591/%E7%BD%91%E9%A1%B53-6%E7%BA%AFhtml/test02.htm?userName=%E8%BF%99%E6%98%AF%E8%B0%81
页面中均能显示“你是谁”
Js中escape,unescape,encodeURI,encodeURIComponent区别:
1.传递参数时候使用,encodeURIComponent不然url中很容易被”#”,”?”,”&”等敏感符号隔断。
2.url跳转时候使用,编码用encodeURI,解码用decodeURI。3.escape() 只是为0-255之外 ASCII字符 作转换工做,转换成的 %u**** 这样的码,若是要用更多的字符如 UTF-8字符库 就必定要用 encodeURIComponent() 或 encodeURI() 转换才能够成 %nn%nn 这的码才能够,其它状况下escape,encodeURI,encodeURIComponent编码结果相同,因此为了全球的统一化进程,在用 encodeURIComponent() 或 encodeURI() 代替 escape() 使用吧!