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? |
|||||||||||||||||
|
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:
|
||||
|
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 |
||||
|
|
||||
|
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. |
||||
|
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 |
||||
|
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
|
||||
|
Nice. To get a specific named argument in the query you could also do it the following way: JavaScript: Get URL Query Argument Remember to url decode the value using the "unescape" method in JavaScript. |
||||
|
This code will create a object which have two method
|
||||
|
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 |
||||
|
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
|
|||||||||||||||||||||
|
Code golf:
Display it!
On my Mac:
|
|||||
|
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. |
|||||
|
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:
|
||||
|
I would rather use split() instead of Regex for this operation:
|
||||
|
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.
|
|||||
|
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:
} |
||||
|
Here's my stab at making Andy E's excellent solution into a full fledged jQuery plugin:
The syntax is:
Best of both worlds! |
||||
|
|
|||||
|
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.