使用JavaScript重定向URL参数

简单实现使用JavaScript重置url参数 ,两种方法,一种字符串拼接形式,一种使用正则判断,且都已经测试通过。供大家参考指教。

1、字符拼接形式

function setUri(para, val) {
            var strNewUrl = new String();
            var strUrl = new String();
            var url = window.location.href;
            strUrl = window.location.href;

            if (strUrl.indexOf("?") != -1) {
                strUrl = strUrl.substr(strUrl.indexOf("?") + 1);   //获取参数

                if (strUrl.toLowerCase().indexOf(para.toLowerCase()) == -1) {  //如果没有找到参数,则直接赋值
                    strNewUrl = url + "&" + para + "=" + val;
                    window.location.href = strNewUrl;
                } else {
                    var aParam = strUrl.split("&");

                    for (var i = 0; i < aParam.length; i++) {
                        if (aParam[i].substr(0, aParam[i].indexOf("=")).toLowerCase() == para.toLowerCase()) {
                            aParam[i] = aParam[i].substr(0, aParam[i].indexOf("=")) + "=" + val;
                        }
                    }
                    strNewUrl = url.substr(0, url.indexOf("?") + 1) + aParam.join("&");
                    window.location.href = strNewUrl;
                }
            } else {
                strUrl += "?" + para + "=" + val;
                window.location.href = strUrl;
            }
        }
View Code

2、利用正则

 1  function setPara(para, val) {
 2             var newpar = "";
 3             var url = window.location.href;
 4             var pars = location.search.substring(1);
 5 
 6             var reg = new RegExp("(^|)" + para + "=([^&]*)(|$)");
 7             if (reg.test(pars)) {   //有需要的参数para
 8 
 9                 var p1 = pars.split(para)[0];   //productID=100857&count=1&
10                 var p2 = pars.split(para)[1];   //=75825&coupval=1.5&addressID=358&invoiceID=1245&invoiceName=jesse
11 
12                 if (p2.indexOf("&") > -1) {
13                     var p3 = p2.split("&")[0];
14                     if (p3 == "=" + val + "") {
15                         return false;
16                     }
17                     newpar = p1 + para + '=' + val + p2.substr(2);
18                 } else {
19                     if (p1) {
20                         newpar = p1 + para + '=' + val;
21                     } else {
22                         newpar = para + '=' + val;
23                     }
24                 }
25             } else {
26                 if (url.indexOf("?") == -1) {  //没有参数
27                     newpar = para + "=" + val;
28                 } else {
29                     newpar = pars + "&" + para + "=" + val;
30                 }
31             }
32             window.location.href = location.href.split('?')[0] + "?" + newpar;
33         }
View Code

 调用: 

<script>
window.onload = function () {
            var btn = document.getElementById("btnClick");

            btn.onclick = function () {

          // url地址:http://localhost:54714/testuri.aspx?productID=100857&count=1&coupresId=12785&coupval=1.5&openId=12456&addressID=358&invoiceID=&invoiceName=
          //setUri("coupresId", 0);
          setPara("coupresId", 0); 
    } 
  }
</script>

<input type="button" id="btnClick" value="重定义参数" />

 

posted @ 2016-10-18 13:20  阿两君  阅读(2962)  评论(0编辑  收藏  举报