JS获取来自URL中的参数

266 阅读1分钟
//获取参数(浏览器URL的)
function getQueryParam(key) {
    var reg = new RegExp("[\?|\&]" + key + "=([^&]+)");
    var mStrs = window.location.search.match(reg);
    return mStrs && decodeURIComponent(mStrs[1]);
}

//获取所有参数(浏览器URL的)
function getQueryParams() {
    var mStrs = window.location.search.match(/[\?\&]\w+=([^&]*)/g);
    var retJson = {}
    for (var i = 0; i < mStrs.length; i++) {
        retJson[mStrs[i].match(/\w+/)[0]] = decodeURIComponent(mStrs[i].split('=')[1]);
    }
    return retJson;
}