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?

4
  • Did you try to produce some code on your own? Commented Aug 2, 2013 at 17:21
  • 1
    Can you be more specific. Commented Aug 2, 2013 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. Commented Aug 2, 2013 at 17:26
  • 1
    See also this Q/A. Commented Apr 28, 2014 at 8:05

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

nice work finding a raw javascript version, however, that's not what you asked for.
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.
Minor point of contention: your .split() uses a RegEx, which you complained about in the question. Not that I'd do it differently. =)
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.
window.location.search, I suppose. (For reasons seen e.g. here, in probably the best guide on URL encoding.)
|
0

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

var queryStringParameter = $.url().param('queryStringParameter');

Comments

0

jQuery BBQ plugin to the rescue. See deparam method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.