封装获取url地址中参数的方法

115 阅读1分钟
// 封装获取url地址中参数的方法
function GetRequest() {
    // bug:decodeURIComponent可解决url中中文参数乱码问题
    var url = decodeURIComponent(location.search); //获取url中"?"符后的字串
    //url = '?invite_code=HPUBU';
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            // bug:decodeURIComponent可解决url中中文参数乱码问题
            theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
        }
    }
    return theRequest;
}
//var Request = new Object();
let invite_code = GetRequest().invite_code;//获取到值
$('#invite_code').val(invite_code);