js获取url中的参数

 

//1.遍历参数逐个比对key
function getQuery(keyValue) {
    var search = location.search.slice(1);
    var arr = search.split("&");
    for (var i = 0; i < arr.length; i++) {
        var item = arr[i];
        var idx = item.indexOf("=");
        var key = item.slice(0,idx);
        var val = item.slice(idx+1);
        //console.log(key,val);
        if (key == keyValue) {
            if (unescape(val) == 'undefined') {
                return "";
            } else {
                return unescape(val);
            }
        }
    }
    return "";
}

//2.用正则表达式去匹配:
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = decodeURI(window.location.search).substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

 



 

posted @ 2019-08-22 11:01  Vaynedy  阅读(816)  评论(0编辑  收藏  举报