html页面跳转传递参数

来源:互联网 发布:node v6.11.1 x64.msi 编辑:程序博客网 时间:2024/05/19 12:40

从a.html跳转到b.html页面,如果给b.html传递参数,可以通过下面的方式来传递:

  1. 直接将参数拼接到url上,如b.html?param1=aa&param2=bb
  2. 通过正则表达式解析参数

    a页面代码:
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>A页面</title></head><body>    <div>这是A页面</div>    <div>        <!-- 参数直接拼接到url上 -->        <a href="b.html?param1=aa&param2=bb">访问b页面</a>    </div></body></html>


b页面代码:

<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>B页面</title></head><body>    <div>这是B页面</div>    <script type="text/javascript">        function getParams(key) {            var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");            var r = window.location.search.substr(1).match(reg);            if (r != null) {                return unescape(r[2]);            }            return null;        };        console.log("参数param1:"+getParams("param1"));//输出aa        console.log("参数param2:"+getParams("param2"));//输出bb    </script></body></html>

getParams方法说明:
- window.location.search为url中参数部分,包括?,如果url为http://localhost/b.html?name=aa&age=20,那么window.location.search的值为 ?name=aa&age=20
- 创建正则表达式,以key开头(如name=aa),或者key前面包含&,并且值的最后有&或直接是末尾,并且值中不包含&

0 0
原创粉丝点击