Previously acquired browser parameters like this
function GetQueryString(name)
{
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null)
return unescape(r[2]);
return null;
}
// 调用方法
alert(GetQueryString("参数名1"));
This method is suitable to obtain a single parameter, but when more than one parameter, when will a lot of trouble, there is no way the browser parameters are placed in an object inside it? So I wrote the following this method
function getParameters() { //获取地址栏参数不包括hash值
var search = decodeURI(location.search);//防止中文乱码
search = search.replace('?', '');
var searray = search.split("&");
var obj = {}
searray.map(function (item) {
var sarry = item.split('=');
obj[sarry[0]] = sarry[1];
})
return obj;
}
var para=getParameters();
It is not very simple, and very convenient
Well, then get a single first time have an advantage, at a time to get all of the advantages, so it can integrate it?
Okay, I went ahead and modifications
function getParameters(obj) {//获取地址栏参数不包括hash值
var search = decodeURI(location.search).replace('?', '');//防止中文乱码
if(typeof obj=="string"){
var reg = new RegExp("(^|&)"+ obj +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null)return unescape(r[2]); return null;
}else if(typeof obj=="undefined"){
var searray = search.split("&");
var obj = {}
searray.map(function (item) {
var sarry = item.split('=');
obj[sarry[0]] = sarry[1];
})
return obj;
}
}
// getParameters();
// getParameters('tn');
Not satisfied that you are not thinking about again I do not want to get a do not want to get all, I wanted to get a few of them do?
How about you make me so love to meet you.
So the final code has become such
function getParameters(obj) {//获取地址栏参数不包括hash值
var search = decodeURI(location.search).replace('?', '');//防止中文乱码
if(typeof obj=="string"){
var reg = new RegExp("(^|&)"+ obj +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null)return unescape(r[2]); return null;
}else if(typeof obj=="undefined"){
var searray = search.split("&");
var obj = {}
searray.map(function (item) {
var sarry = item.split('=');
obj[sarry[0]] = sarry[1];
})
return obj;
}else if(obj.constructor ==Array){
var searray = search.split("&");
var fromarray = {}
searray.map(function (item) {
var sarry = item.split('=');
if(obj.indexOf(sarry[0])>-1){
fromarray[sarry[0]] = sarry[1];
}
})
return fromarray;
}
}
// getParameters();
// getParameters('tn');
//getParameters(['tn','wd']);
muah.