Is there a plugin-less way of retrieving query string values via jQuery (or without)?
If so, how, and if not what plugin do you recommend?
Is there a plugin-less way of retrieving query string values via jQuery (or without)? If so, how, and if not what plugin do you recommend? |
|||||||||||||||||
|
You don't need jQuery for that purpose. You can use pure JavaScript:
|
|||||||||||||||||||||
|
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. 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) (jsPref)Preparation code: methods declaration Split test code
Regex test code
Testing in Firefox 4.0 x86 on Windows Server 2008 R2 / 7 x64
|
|||||||||||||||||||||
|
Improved version of Artem Barger's answer:
For more information on improvement see: http://james.padolsey.com/javascript/bujs-1-getparameterbyname/ |
|||||
|
Just another recommendation. The plugin jQuery-URL-Parser allows to retrieve all parts of URL, including anchor, host, etc. Usage is very simple and cool:
|
|||||
|
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. |
|||||
|
If you're using jQuery, you can use a library, such as jQuery BBQ: Back Button & Query Library.
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. |
||||
|
Here's my stab at making Andy E's excellent solution into a full fledged jQuery plugin:
The syntax is:
Best of both worlds! |
||||
|
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 |
||||
|
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. |
||||
|
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. |
||||
|
Use the following to get a query param value given a key name:
|
||||
|
|
|||||
|
These are all great answers, but I needed something a bit more robust, and thought you all might like to have what I created. It is a simple library method that does dissection and manipulation of url parameters. The static method has the following sub methods that can be called on the subject url:
Example:
|
||||
|
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 |
||||
|
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:
} |
||||
|
Code golf:
Display it!
On my Mac:
|
|||||
|
Here is a fast way to get an object similar to the PHP $_GET array:
Usage:
For the query string
|
||||
|
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.
|
|||||
|
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 |
||||
|
Keep it simple:
Call it from anywhere in the JavaScript code:
|
||||
|
From developer.mozilla.org:
|
||||
|
I like this one:
Works great for me. |
|||||
|
I developed a small library using techniques listed here to create an easy to use, drop-in solution to anyones troubles; It can be found here: https://github.com/Nijikokun/query-js Usage Fetching specific parameter/key:
Using the builder to fetch the entire object:
and tons more... check the github link for more examples. Features
|
||||
|
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
|
||||
|
Code:
How to Call:
Output:
|
||||
|
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
|
||||
|
And this is how you can use this function assuming the URL is
|
||||
|
One line code to get Query
|
|||||
|
This the most simple and small function JavaScript to get int ans String parameter value from URL
Source And DEMO : http://bloggerplugnplay.blogspot.in/2012/08/how-to-get-url-parameter-in-javascript.html |
||||
|
This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.