3

I've seen numerous solutions that utilize RegEx, and to be quite frank, that seems ridiculously excessive since javascript is so versatile.

There must be a simpler way to access request parameters.

Could somebody demonstrate for me?

Michael
  • 8,362
  • 6
  • 61
  • 88
keeehlan
  • 7,874
  • 16
  • 56
  • 104
  • Did you try to produce some code on your own? – kul_mi Aug 02 '13 at 17:21
  • 1
    Can you be more specific. – rozar Aug 02 '13 at 17:21
  • I believe OP refers to the query string attached to the URL of the current page. Clarifying that _might_ help, but really it seems clear enough to me. –  Aug 02 '13 at 17:26
  • 1
    See also [this Q/A](http://stackoverflow.com/questions/831030/how-to-get-get-request-parameters-in-javascript). – Sz. Apr 28 '14 at 08:05

3 Answers3

4

I found a useful method in the depths of the net.

function querySt(Key) {
    var url = window.location.href;
    KeysValues = url.split(/[\?&]+/);
    for (i = 0; i < KeysValues.length; i++) {
        KeyValue = KeysValues[i].split("=");
        if (KeyValue[0] == Key) {
            return KeyValue[1];
        }
    }
}

Downvotes and plugins aside, thanks anyways.

keeehlan
  • 7,874
  • 16
  • 56
  • 104
  • 2
    nice work finding a raw javascript version, however, that's not what you asked for. – Mike Ramsey Oct 08 '13 at 19:34
  • 1
    I actually like the pure javascript version, as it doesn't require loading in some additional library. Either way, glad you came up with a solution. – Mike Ramsey Oct 09 '13 at 13:06
  • 1
    Minor point of contention: your `.split()` uses a RegEx, which you complained about in the question. Not that I'd do it differently. =) – Michael Oct 18 '13 at 16:19
  • This works in my case but I notice this doesn't handle arrays - what if there are multiple values for one parameter (i.e. param1=val1&param1=val2&param1=val3)? Neat solution which works for most situations in any case. – ClarkeyBoy Mar 03 '14 at 12:10
  • 1
    `window.location.search`, I suppose. (For reasons seen e.g. [here, in probably the best guide on URL encoding](http://blog.lunatech.com/2009/02/03/what-every-web-developer-must-know-about-url-encoding).) – Sz. Apr 28 '14 at 08:02
  • forgot to `return "";` or null at the end of the function in case key is not found. – jmbmage Nov 05 '14 at 16:54
0

You can use jQuery plugin Purl (A JavaScript URL parser) v2.3.1

var queryStringParameter = $.url().param('queryStringParameter');
Adil
  • 146,340
  • 25
  • 209
  • 204
0

jQuery BBQ plugin to the rescue. See deparam method

Mike Ramsey
  • 843
  • 8
  • 23