使用js修改url地址参数

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

例如将

  www.baidu.com

修改为

  www.baidu.com?name=123

操作为:

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

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; 
        } 
    }
}

 

 

转自:https://www.cnblogs.com/summer0319/p/7133547.html

posted @ 2018-08-06 16:39  James2019  阅读(7154)  评论(0编辑  收藏  举报