Url参数获取和更改

获取多个url参数
1
2
3
4
5
6
7
8
9
10
getUrlParam:function (name){
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null)
return unescape(r[2]);
return null;
}
```
##### 获取单个url参数

getUrlParam:function (name){
var params = location.search.substr(1);
var ArrParam = params.split(‘&’);
for(var i = 0; i <ArrParam.length; i ++){
if(ArrParam[i].split(‘=’)[0] == name){
return ArrParam[i].split(‘=’)[1];
}
}
}

1
2
##### 修改url地址

changeURLPar:function(url,arg,arg_val){
var pattern=arg+’=([^&])’;
var replaceText=arg+’=’+arg_val;
if(url.match(pattern)){
var tmp=’/(‘+ arg+’=)([^&]
)/gi’;
tmp=url.replace(eval(tmp),replaceText);
return tmp;
}else{
if(url.match(‘[?]’)){
return url+’&’+replaceText;
}else{
return url+’?’+replaceText;
}
}
return url+’\n’+arg+’\n’+arg_val;
}
//用法
var txt = window.location.href
changeURLPar(txt, ‘type’, 99)
```