首页 > 代码库 > 将url的查询参数解析成字典对象

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

1、先截取请求参数字符串;

2、使用decodeURIComponent函数进行解码;

3、正则匹配出参数对象;

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, key, value) {         var name = decodeURIComponent(key);         var val = decodeURIComponent(value);                        val = String(val);         obj[name] = val;         return rs;     });     return obj; } 

 

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