方法一、获取参数数组
function getQueryString() {
var qs = location.search.substr(1), // 获取url中"?"符后的字串
args = {}, // 保存参数数据的对象
items = qs.length ? qs.split("&") : [], // 取得每一个参数项,split() 方法用于把一个字符串分割成字符串数组。
item = null,
len = items.length;
for(var i = 0; i < len; i++) {
item = items[i].split("=");
var name = decodeURIComponent(item[0]),//decodeURIComponent() 函数可对 encodeURIComponent() 函数编码的 URI 进行解码。
value = decodeURIComponent(item[1]);
if(name) {
args[name] = value;
}
}
return args;
}
getQueryString();
console.log(getQueryString())
如链接为 http://localhost:8088/test.html?name=abc&&id=123
则返回结果为{name:“abc”,id:“123”}
方法二、获取某个特定参数
function getQueryStringsss(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if(r != null) {
return decodeURIComponent(r[2]);
}
}
console.log(getQueryStringsss('id'));//123
如链接为 http://localhost:8088/test.html?name=abc&&id=123
则返回结果为123