Location.search返回的字符串以‘ ?’开头,各参数间以‘&’相连,如url?a=1&b=2
//解析查询字符串
function getQueryStringArgs() {
//取得查询字符串,并去掉开头'?'
var qs = location.search.length?location.search.substring(1):'';
//保存数据的对象
var args={},
//以分割符'&'分割字符串,并以数组形式返回
items = qs.length?qs.split('&'):[],
item=null,
name=null,
value=null,
i=0,
len = items.length;
//逐个将每一项添加到args对象中
for(;i<len;i++) {
item = items[i].split('=');
//解码操作,因为查询字符串经过编码的
name = decodeURIComponent(item[0]);
value = decodeURIComponent(item[1]);
value = item[1];
if(name.length) {
args[name] = value;
}
}
return args;
}