Is there a plugin-less way of retrieving query string values via jQuery (or without)?
If so, how? If not, is there a plugin which can do so?
Is there a plugin-less way of retrieving query string values via jQuery (or without)? If so, how? If not, is there a plugin which can do so? |
|||||||||||||||||
locked by animuson♦ Jul 25 '14 at 19:35This question's answers are a collaborative effort: if you see something that can be improved, just edit the answer to improve it! No additional answers can be added here |
|||||||||||||||||
|
You don't need jQuery for that purpose. You can use just some pure JavaScript:
Usage:
|
|||||||||||||||||||||
|
If you're using jQuery, you can use a library, such as jQuery BBQ: Back Button & Query Library.
Edit: Adding Deparam Example:
If you want to just use plain JavaScript, you could use...
Because of the new HTML History API and specifically This version will update its internal cache of parameters each time the history changes. |
||||
|
Roshambo on snipplr.com has a really hot and simple script to achieve this described in Get URL Parameters with jQuery | Improved. With his script you also easily get to pull out just the parameters you want. Here's the gist:
Then just get your parameters from the query string. So if the URL/query string was Just call UZBEKJON has a great blog post on this as well, Get URL parameters & values with jQuery. |
|||||||||
|
Some of the solutions posted here are inefficient. Repeating the regular expression search every time the script needs to access a parameter is completely unnecessary, one single function to split up the parameters into an associative-array style object is enough. If you're not working with the HTML 5 History API, this is only necessary once per page load. The other suggestions here also fail to decode the URL correctly.
Example querystring:
Result:
This could easily be improved upon to handle array-style query strings too. An example of this is here, but since array-style parameters aren't defined in RFC 3986 I won't pollute this answer with the source code. For those interested in a "polluted" version, look at campbeln's answer below. Also, as pointed out in the comments, If you're using a server-side preprocessing language, you might want to use its native JSON functions to do the heavy lifting for you. For example, in PHP you can write:
Much simpler! |
|||||||||||||||||||||
|
|
|||||
|
Without jQuery
With an URL like
Google methodTearing Google's code I found the method they use:
It is obfuscated, but it is understandable. They start to look for parameters on the url from In the end the object My method as a jQuery plugin
Usage
Performance test (split method against regex method) (jsPerf)Preparation code: methods declaration Split test code
Regex test code
Testing in Firefox 4.0 x86 on Windows Server 2008 R2 / 7 x64
|
|||||||||||||||||||||
|
Here's my stab at making Andy E's excellent solution into a full fledged jQuery plugin:
The syntax is:
Best of both worlds! |
||||
|
Here is my version of query string parsing code on github It's "prefixed" with jquery.*, but the parsing function itself don't use jQuery. Its pretty fast but still open for few simple performance optimizations. Also it supports list & hash-tables encoding in URL, like:
or
|
||||
|
Code golf:
Display it!
On my Mac:
|
|||||
|
Roshambo jQuery method wasn't taking care of decode URL http://snipplr.com/view/26662/get-url-parameters-with-jquery--improved/ Just added that capability also while adding in the return statement
Now you can find the updated gist:
|
||||
|
This is a function I created a while back and I'm quite happy with. It is not case sensitive - which is handy. Also, if the requested QS doesn't exist, it just returns an empty string. I use a compressed version of this. I'm posting uncompressed for the novice types to better explain what's going on. I'm sure this could be optimized or done differently to work faster, but it's always worked great for what I need. Enjoy.
|
|||||
|
Improved version of Artem Barger's answer:
For more information on improvement see: http://james.padolsey.com/javascript/bujs-1-getparameterbyname/ |
|||||||||||||
|
I use regular expressions a lot but not for that. It seems easier and more efficient to me to read the query string once in my application, and build an object from all the key/value pairs like:
For an URL like |
||||
|
Just another recommendation. The plugin Purl allows to retrieve all parts of URL, including anchor, host, etc. It can be used with or without jQuery. Usage is very simple and cool:
|
|||||||||||||||||
|
I would rather use split() instead of Regex for this operation:
|
||||
|
I took this answer and added support for optionally passing the URL in as a parameter; falls back to window.location.search. Obviously this is useful for getting the query string parameters from URLs that are not the current page:
|
||||
|
Doesn't have enough reputation for comment, sigh. Here's my edit to this excellent answer - with added ability to parse query strings with keys without values.
IMPORTANT! Parameter for that func in last line is different, it's just example how one can pass arbitrary url to it. You can use last line from Bruno answer to parse current url. So what exactly changed? With url |
||||
|
This code will create a object which have two method
|
||||
|
Try this:
Then call it like so:
You can use this for cookies also:
This only works for strings that have "key=value[&|;|$]"... will not work on objects/arrays. If you don't want to use String.prototype... move it to a function and pass the string as an argument |
||||
|
I like Ryan Phelan's solution. But I don't see any point of extending jQuery for that? There is no usage of jQuery functionality. On other hand I like the built-in function in Google Chrome: window.location.getParameter. So why not to use this? Okay, other browsers don't have. So let's create this function if it does not exist:
This function is more or less from Ryan Phelan, but it is wrapped differently: clear name and no dependencies of other javascript libraries. More about this function on my blog. |
|||||||||
|
|
||||
|
Keep it simple in plain javascript:
Call it from anywhere in the JavaScript code:
|
|||||
|
If you're doing more URL manipulation than simply parsing the querystring, you may find URI.js helpful. It is a library for manipulating URLs - and comes with all the bells and whistles. (Sorry for self-advertising here) to convert your querystring into a map:
(URI.js also "fixes" bad querystrings like |
||||
|
Here's my own take on this. This first function decodes a URL string into an object of name/value pairs:
And as an added bonus, if you change some of the args, you can use this second function to put the array of args back into the URL string:
|
|||||
|
Code:
How to Call:
Output:
|
||||
|
This one works fine
taken from here |
|||||
|
The following function returns an object version of your queryString.
You can simply write
And to use this function you can write
|
||||
|
I like this one:
Works great for me. |
||||
|
Use the following to get a query param value given a key name:
Use a stronger regex if you have a common substring problem:
Firefox supports the URLSearchParams API, but it is not standardized...yet. |
||||
|
Here is a fast way to get an object similar to the PHP $_GET array:
Usage:
For the query string
|
|||||
|
Thank you for your interest in this question.
Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?