//获得参数(只对字母数字等有效,参数值为中文则不能传) function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; } //获取参数2(对数字、字母、中文均有效.但在对URL进行escape()编码再解码unescape()时不能使用,只能用上述方法) function getQueryString2(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if(r != null) return decodeURI(r[2]); return null; } var userid = getQueryString('userid'); var username=getQueryString2('username'); console.log(userid); console.log(username);
// 方法二: function getQueryString() { var obg={},a='';
(a=window.location.search.substr(1))|| (a=window.location.hash.split('?')[1])
a.split(/&/g).forEach(function(item){
obg[(item=item.split('='))[0]]=item[1];
})
return obg ;
}
getQueryString();
// 类似方法二
function spiltUrl(src){
let data = {};
// let src = window.location.href
let index = src.indexOf("?");
if(index===-1){
return data;
}
let dataStr = src.substring(src.indexOf("?")+1);
console.log(dataStr)
let dataArray = dataStr.split("&");
for(let i=0;i<dataArray.length;i++){
let param = dataArray[i].split("=");
data[param[0]]=param[1];
}
// this.urlData = data
console.log(data)
return data ;
}
spiltUrl('http://vip.qq.com/a.php?a=1&b=3&c=4&d=5');
// 方法3(雷同方法一)
function getQueryString(e) {
var t = new RegExp("(^|&)" + e + "=([^&]*)(&|$)");
var a = window.location.href.match(t);
if (a != null) return a[2];
return ""
}