1

Given an Url like this: http://mydomain.com/mypage.asp?Animal=Elephant

VBScript provides a nice way to get individual items in a QueryString using the Request object:

dim animal = Request.QueryString["Animal"]

I've seen some rather convoluted JavaScript floating around to do something similar, but I would hope that the jQuery library would have a function to do this without having to reinvent some wheel. I can't find it in the jQuery book I have, nor on the jQuery website.

Am I barking up the tree at nothing? Is there a NATIVE jQuery function to do this?

Please note that my question is asking for the existence of a NATIVE JQUERY FUNCTION to get a QueryString which is comparable to the VBScript I provided above. A ONE LINE SOLUTION to the question. Such as:

var animal = $.getParameterByName("Animal");

There are only 2 possible answers to my question. They are:

  1. No, there isn't a native one-line jQuery function as described, or
  2. Yes, there is a native one-line jQuery function as described, and here it is...

I know how to get the entire QueryString and how to parse it using Javascript - what I don't know is if there is a jQuery method or function that does what I ask.

Cyberherbalist
  • 12,061
  • 17
  • 83
  • 121
  • without a plugin, no there isn't. But it isn't too difficult to make on your own, just take the window.location and split it on `?`, then again on `&`, and then finally on `=` – Kevin B Oct 17 '13 at 21:09
  • 2
    use javascript => http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values – PlantTheIdea Oct 17 '13 at 21:11
  • Sigh. Yes, @KevinB I know how to do it using Javascript already. I want to know if there is a one-liner in the jQuery library that does it. See my revised question. – Cyberherbalist Oct 17 '13 at 22:03
  • @PlantTheIdea see my revised question. – Cyberherbalist Oct 17 '13 at 22:04
  • @Cyberherbalist my original comment answers that: "*without a plugin, no there isn't.*" – Kevin B Oct 17 '13 at 22:04
  • 1
    I saw that @KevinB, and thankyou, but I was responding that I already know how to pull the window.location and parse a string using splits in Javascript. If your answer is "No there isn't" then why don't you provide an Answer that says so instead of a comment? Just sayin'. I was just hoping that despite all evidence to the contrary that there was a native jQuery function as described. I am surpised actually that there isn't one. – Cyberherbalist Oct 17 '13 at 22:11

2 Answers2

1

"No, there isn't a native one-line jQuery function as described"

However, it's very easy to change that.

(function($){
    function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    $.getParameterByName = getParameterByName;
})(jQuery);

with that included in the head after jquery, you can now simply do:

var animal = $.getParameterByName("Animal");

Method for getting parameter by name taken from: How can I get query string values in JavaScript?

Community
  • 1
  • 1
Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • That's what I needed! "No there isn't, but here's how to remedy it." I was already aware of that particular routine (got it from the same place, in fact), but thanks bunches for packaging it in a "jQuery-like" form. Just what the Dr. ordered. – Cyberherbalist Oct 17 '13 at 22:20
0

I know this doesn't answer you question, but I agree that the VBScript way does feel nice.

window.Request = {
  QueryString: {}
};

window.location.search.split('?')[1].split('&').forEach(function(kv){
  Request.QueryString[kv.split('=')[0]] = decodeURIComponent(
    kv.split('=')[1].replace(/\+/g, " ")
  );
});

var animal = Request.QueryString['animal'];
tjb1982
  • 2,257
  • 2
  • 26
  • 39