将url的查询参数解析成字典对象

function getQueryObject(url) {
    url = url == null ? window.location.href : url;
    var search = url.substring(url.lastIndexOf("?") + 1);
    var obj = {};
    var reg = /([^?&=]+)=([^?&=]*)/g;
    search.replace(reg, function (rs, $1, $2) {
        var name = decodeURIComponent($1);
        var val = decodeURIComponent($2);                
        val = String(val);
        obj[name] = val;
        return rs;
    });
    return obj;
}


例如:

  var url = 'http://www.xxx.com?ProID=100&UserID=200'
  getQueryObject(url)
输出为:

  

 

posted @ 2018-05-14 15:53  adolfvicto  阅读(257)  评论(0编辑  收藏  举报