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 at 19:35This question's answer is a collaborative effort: if you see something that can be improved, just edit 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:
|
|||||||||||||||||||||
|
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:
|
|||||||||||||||||
|
tl;dr solution using vanilla JavaScriptTo access different parts of url use
multi-valued keysSimple key check
encoded characters?Enclose the
|
|||||||||
|
This function will return a parsed JavaScript object with any arbitrarily nested values using recursion as necessary. Here's a jsfiddle example.
Given any of the above test examples.
|
||||
|
|
||||
|
|
||||
|
Why not just use 2 splits ?
I was reading all previous and more complete answer. But I think that is the simplest and faster method. You can check in this jsPerf benchmark To solve the problem in Rup's comment, add a conditional split by changing the first line to the two below. But absolute accuracy means it's now slower than regexp (see jsPerf).
So if you know you won't run into Rup's counter-case, this wins. Otherwise, regexp. |
|||||
|
Most pretty but basic:
It doesn't handle values lists such as |
||||
|
Amazing how many overly complicated and incomplete solutions are posted here. Here's what I'm using:
Works with following urls:
Returns an empty string in the first 4 cases, and the string '?&=' in the last two cases. Returning '?&=' rather than 'ignored' in the last case makes it consistent with PHP. Works in IE7. |
||||
|
Here's an extended version of Andy E's linked "Handle array-style query strings"-version. Fixed a bug ( It will handle the following querystring...
...making it into an object that looks like...
As you can see above, this version handles some measure of "malformed" arrays, i.e. -
It seems the jury is somewhat out on repeated keys as there is no spec. In this case, multiple keys are stored as an (fake)array. But do note that I do not process values based on commas into arrays. The code:
|
||||
|
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! |
|||||||||||||||||||||
|
Here is String prototype implementation:
Example call:
|
||||
|
For those who wants a short method (with limitations):
|
|||||
|
quick, easy, and fast: The Function:
Usage:
|
||||
|
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. |
|||||
|
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
|
|||||||||||||||||||||
|
The shortest possible expression in terms of size to obtain a query object seems to be:
You can make use of the
|
||||
|
This didn't work for me, I want to match `?b' as the 'b' paramter is present, and not match '?return' as the 'r' paramrter, here is my solution.
|
||||
|
Simple Solution with Plain JS and Regex
|
||||
|
Get all querystring parameters including checkbox values (arrays). Considering the a correct & normal use of GET parameters the things i see it's missing, on most functions, is the support for arrays and removing the hash data So i wrote this function
Using shorthand operators & while-- loop the performance should be very good to. Support:
Notes: It does not support object arrays (key[key]=value) If the space is + it remains a +. add Usage:
Return:
Demo: Info If you don't understand something or you can't read the function just ask i'm happy to explain what i did here. If you think the function is unreadable and unmanainable i'm happy to rewrite the function for you , but consider that shorthand & bitwise operators are always faster than a standard syntax (mybe read about shorthands and bitwise operators in the ECMA-262 book or us your favorite searchengine).Rewriting the code in a standard readable syntax means performance loss. |
||||
|
this will work.. You need call this function where you need get the parameter by passing its name..
|
|||||
|
This will parse variables AND arrays from a URL string. It uses neither regex or any external library.
Example:
Output:
|
||||
|
Time for the magic of simplicity...
And if you need to handle
Note that given a URL like |
|||||
|
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:
|
||||
|
And this is how you can use this function assuming the URL is
|
||||
|
From the MDN:
|
||||
|
Use the following to get a query param value given a key name:
|
||||
|
Keep it simple in plain javascript:
Call it from anywhere in the JavaScript code:
|
||||
|
Not to beat a dead horse, but if you have underscore or lodash, a quick and dirty way to get this done is:
|
|||||
|
I'm gonna throw my hat in the ring - I needed an object from the query string, and I hate lots of code. may not be the most robust in the universe but it's just a few lines of code.
a URL like
|
|||||||||
|
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?