Home  >  Article  >  Web Front-end  >  Summary of two methods for redirecting URL parameters in JavaScript

Summary of two methods for redirecting URL parameters in JavaScript

高洛峰
高洛峰Original
2017-03-23 15:48:542153browse

This article mainly introduces two methods of redirecting URL parameters in JavaScript. I won’t say much below, just look at the sample code.

1. Character splicing form

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

2. Using regular

//使用正则
  function setPara(para, val) {
    var newpar = "";
    var url = window.location.href;
    var pars = location.search.substring(1);
 
    var reg = new RegExp("(^|)" + para + "=([^&]*)(|$)");
    if (reg.test(pars)) {  //有需要的参数para
 
      var p1 = pars.split(para)[0];  //productID=100857&count=1&
      var p2 = pars.split(para)[1];  //=75825&coupval=1.5&addressID=358&invoiceID=1245&invoiceName=jesse
 
      if (p2.indexOf("&") > -1) {
        var p3 = p2.split("&")[0];
        if (p3 == "=" + val + "") {
          return false;
        }
        newpar = p1 + para + &#39;=&#39; + val + &#39;&&#39; + (p2.split(p3))[1];
      } else {
        if (p1) {
          newpar = p1 + para + &#39;=&#39; + val;
        } else {
          newpar = para + &#39;=&#39; + val;
        }
      }
    } else {
      if (url.indexOf("?") == -1) {
        newpar = pars +"&"+ para + "=" + val;
      } else {
        newpar = pars + "&" + para + "=" + val;
      }
    }
    window.location.href = location.href.split(&#39;?&#39;)[0] + "?" + newpar;
  }

Call:

<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="重定义参数" />

Related articles:

Three methods of PHP page redirection

The difference between PHP redirection and pseudo-static

Redirect the external links of the website

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn