function changeURLArg(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+' '+arg+' '+arg_val; }
使用方法
var urldest=changeURLArg(window.location.href,"userid","1");
无刷新设置当前页面地址
history.pushState(null,null,urldest);