javaScript不同页面间传递参数

来源:互联网 发布:美国人工智能机器人 编辑:程序博客网 时间:2024/05/17 08:11

1传递参数的页面test01.htm

<script type="text/javascript">
    function send() {
        var url = "test02.htm"; 
        var userName = "这是谁";//假设参数值为这是谁

        window.open(encodeURI(url + "?userName=" + userName));

        //encodeURI编码
    }

</script>
<input id="btn" onclick="send()" value="点击" type="button" name="button"/>

 

2接受并显示参数页面test02.htm

<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解码

</script>

在浏览器中运行test01.htm  点击按钮,进入test02.htm

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() 使用吧!

原创粉丝点击