Two methods of JavaScript redirection URL parameters summary _javascript tips

Source: Internet
Author: User

This article is mainly about the JavaScript redirect URL parameters of two ways, the following is not much to say, see the sample code directly.

One, the 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);  Gets the parameter if (Strurl.tolowercase (). IndexOf (Para.tolowercase ()) = = 1) {//If no parameters are found, direct assignment 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; }
    }

II, using regular

  Use 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)) {//have the required parameters 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 + ' = ' + val + ' & ' + (P2.split (p3)) [1];
          else {if (p1) {newpar = p1 + para + ' = ' + val;
          else {Newpar = para + ' = ' + val;
        }} else {if (Url.indexof ("?") = = 1) {Newpar = pars + "&" + para + "=" + val; else {Newpar = pars +"&" + para + "=" + val; } window.location.href = Location.href.split ('? ')
    [0] + "?" + Newpar; }

Call:

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

      Btn.onclick = function () {

//URL address: Http://localhost:54714/testuri.aspx?productID=100857&count=1&co Upresid=12785&coupval=1.5&openid=12456&addressid=358&invoiceid=&invoicename=
//setUri (          "Coupresid", 0);
Setpara ("Coupresid", 0); 
}
</script>

<input type= "button" id= "Btnclick" value= "Redefine parameters"/>

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.