使用js修改url地址参数

 

该方法可以修改url的参数。

例如将

  www.baidu.com

修改为

  www.baidu.com?name=123

操作为:

  window.location.href = changeURLArg(window.location.href,'name',123)

 

 1 function changeURLArg(url,arg,arg_val){
 2     var pattern=arg+'=([^&]*)';
 3     var replaceText=arg+'='+arg_val; 
 4     if(url.match(pattern)){
 5         var tmp='/('+ arg+'=)([^&]*)/gi';
 6         tmp=url.replace(eval(tmp),replaceText);
 7         return tmp;
 8     }else{ 
 9         if(url.match('[\?]')){ 
10             return url+'&'+replaceText; 
11         }else{ 
12             return url+'?'+replaceText; 
13         } 
14     }
15 }

 

posted @ 2017-07-07 18:04  rrranmo  阅读(23932)  评论(1编辑  收藏  举报