js获取url参数 数组


function GetRequest() {
    var url = decodeURIComponent(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++) {
            var tempArr = strs[i].split("=");
            // 类型转换
            if (tempArr[1] == 'true') {
                tempArr[1] = true;
            }
            if (tempArr[1] == 'false') {
                tempArr[1] = false;
            }
            if (/^[\d|.]+$/.test(tempArr[1])) {
                tempArr[1] = Number(tempArr[1]);
            }
            // 写入对象
            if (tempArr[0].search(/\[.*]/) == -1) {
                theRequest[tempArr[0]] = tempArr[1];
            } else {
                // 数组
                var key = tempArr[0].replace(/\[.*]/, '');
                if (!theRequest[key]) {
                    theRequest[key] = [tempArr[1]];
                } else {
                    theRequest[key].push(tempArr[1]);
                }
            }
        }
    }
    return theRequest;
}
posted @ 2021-02-01 14:06  DoublePD  阅读(323)  评论(0编辑  收藏  举报