【工具函数】
<h4>工具函数 : 查询URL字符串参数</h4>
<hr />
<p>返回包含所有参数的一个对象</p>
<pre><code>/**
* 工具函数:查询URL字符串参数
* @return { Object }
*/
var url = window.location.search; //返回URL的查询字符串。这个字符串以问号开头
console.log(url) //?a=aa&b=bb
function getQueryStringArgs() {
//取得查询字符串并去掉开头的问号
var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
//保存数据的对象
args = {},
//取得每一项
items = qs.length ? qs.split("&") : [],
item = null,
name = null,
value = null,
//在 for 循环中使用
i = 0,
len = items.length;
//逐个将每一项添加到 args 对象中
for (i = 0; i < len; i++) {
item = items[i].split("=");
name = decodeURIComponent(item[0]);
value = decodeURIComponent(item[1]);
if (name.length) {
args[name] = value;
}
}
return args;
}
var obj = getQueryStringArgs();
console.log(obj) //{a:'aa',b:'bb'}</code></pre>