Get URL parameters quickly using JavaScript

Source: Internet
Author: User

First of all: the originalQuick tip:get URL Parameters with JavaScript
 function Getallurlparams (URL) {var queryString = URL? url.split ('? ')            [1]: Window.location.search.slice (1);            var obj = {};                if (queryString) {queryString = Querystring.split (' # ') [0];                var arr = querystring.split (' & ');                    for (var i = 0; i < arr.length; i++) {var a = arr[i].split (' = ');                    var paramnum = undefined;                        var paramname = a[0].replace (/\[\d*\]/, function (v) {ParamNum = V.slice (1,-1);                    Return ';                    }); var paramvalue = typeof (A[1]) = = = ' undefined '?                    "": a[1];                    ParamName = Paramname.tolowercase ();                    Paramvalue = Paramvalue.tolowercase (); if (Obj[paramname]) {if (typeof obj[paramname] = = = ' String ') {Obj[para                        Mname] = [Obj[paramname]];   }                     if (typeof ParamNum = = = ' undefined ') {Obj[paramname].push (paramvalue);                        } else {Obj[paramname][paramnum] = paramvalue;                    }} else {obj[paramname] = paramvalue;        }}} return obj; }

  

It's a little bit of a change here.
      Set parameter value (use ' true ' if empty)      var paramvalue = typeof (A[1]) = = = ' undefined '? True:a[1];      (optional) Keep case consistent      paramname = Paramname.tolowercase ();      Paramvalue = Paramvalue.tolowercase ();

Optionally, you can set all parameter names and values to lowercase. That's the, can avoid situations where someone sends traffic to a URL with example=TRUE instead of and example=true your script breaks (I ' ve seen this happen). However, if your query string needs to is case sensitive, feel free to omit this part

= paramName.toLowerCase();paramValue = paramValue.toLowerCase();

In the original text, a Boolean value is returned if the Parameter object value is empty. But obviously paramvalue.tolowercase () does not allow you to do so.

The whole feeling is pretty good ~ ~

--------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------

Above

Get URL parameters quickly using JavaScript

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.