删除URL指定的参数
//获取和设置url参数
赋值 继续在追加URL后面追加值
//如果不想追加 那么先调用 $.UrlParamDel
$.UrlParams = function (url , name , value ) {
//var reg=new RegExp("(\\? | \\& )" + name + "=([^& ]+)( & | $ )" , "i");
var patter1 = "(\\?)" + name + "=([^&]+)(&|$)";
var patter2 = "(\\&)" + name + "=([^&]+)(&|$)";
if(typeof value != 'undefined'){ //赋值 继续在追加URL后面追加值
if(url.indexOf('?')==-1){
return (url+'?'+name+'='+value);
}else{
return (url+'&'+name+'='+value);
}
} else { //取值
var reg=new RegExp(patter1, "i");
var m = url.match(reg);
if(m) {
return m[2];
}else{
reg=new RegExp(patter2,"i");
m = url.match(reg);
if(m)
return m[2];
}
return '';
}
}
//删除url指定名称的参数
$.UrlParamDel=function(url ,name){
/*
var reg=new RegExp("(\\\? | &)" + name + "=([^&]+)(&|$)", "i");
//var reg=new RegExp("(\\\? | &)pageIndex=([^&]+)(&|$)", "i");
var m = url.match(reg);
alert(m);
return url.replace(reg,"");*/
var patter1 = "(\\?)" + name + "=([^&]+)(&|$)";
var patter2 = "(\\&)" + name + "=([^&]+)(&|$)";
var reg=new RegExp(patter1, "i");
var m = url.match(reg);
url=url.replace(reg,"?");
reg=new RegExp(patter2,"i");
url = url.replace(reg,"");
return url;
}
测试:
function test() {
// 设置 pageIndex, pageSize 参数到 url
var url2=location.href;
alert(url2);
url2=$.UrlParamDel(url2,"pageIndex");
url2 = $.UrlParams(url2,"pageIndex",e.pageIndex);
url2 = $.UrlParams(url2,"pageSize",e.pageSize);
alert(url2);
var v = $.UrlParams(url2,"pageIndex");
alert(v);
}
测试成功
good luck!
URL参数读取或设置 删除
最新推荐文章于 2023-12-11 14:41:33 发布