JS 获取url参数

<html>
<body>

浏览器中输入 http://localhost/test/js_url.php?a1=aaa&a2=bbb&a3=ccc
会依次弹出aaa,bbb,ccc

<script type="text/javascript">
//方法一
function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}

// 调用方法
alert(GetQueryString("a1"));
alert(GetQueryString("a2"));

//方法二
function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}
var Request = new Object();
Request = GetRequest();
alert(Request['a3']);

</script>
</body>
</html>

 

posted @ 2016-10-24 21:37  风吹屁股凉冰冰  阅读(371)  评论(0编辑  收藏  举报