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?
We started with Q&A. Technical documentation is next, and we need your help.
Whether you're a beginner or an experienced developer, you can contribute.
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 |
|||||||||||||||||||||
|
Code:
How to Call:
Output:
|
||||
|
If you are using Browserify, you can use the
Further reading: URL Node.js v0.12.2 Manual & Documentation |
|||||||||||||||||||||
|
One-liner to get the query:
|
|||||||||||||
|
Here's what I'm using:
Returning 'override' rather than '0' in the last case makes it consistent with PHP. Works in IE7. |
|||||||||||||||||||||
|
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. It's pretty fast, but still open for few simple performance optimizations. Also it supports list & hash-tables encoding in the URL, like:
or
|
|||||
|
For those who wants a short method (with limitations):
|
|||||||||||||
|
Get all querystring parameters including checkbox values (arrays). Considering the 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 unmaintainable I'm happy to rewrite the function for you, but consider that shorthand & bitwise operators are always faster than a standard syntax (maybe read about shorthands and bitwise operators in the ECMA-262 book or use your favorite search engine). Rewriting the code in a standard readable syntax means performance loss. |
|||||||||||||
|
|
||||
|
This one works fine
taken from here |
|||||||||||||
|
A very lightweight jQuery method:
And to alert, for example ?q
|
|||||||||
|
I would rather use
|
||||
|
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:
|
|||||||||||||
|
The following function returns an object version of your queryString.
You can simply write
And to use this function you can write
|
||||
|
If you have Underscore.js or lodash, a quick and dirty way to get this done is:
|
|||||||||||||
|
Try this:
Then call it like so:
You can use this for cookies also:
This only works for strings that have If you don't want to use String.prototype... move it to a function and pass the string as an argument |
||||
|
This function converts the querystring to a JSON-like object, it also handles value-less and multi-value parameters:
We perform a check for Usage:
|
||||
|
There is a nice little
Check out more examples and download here: https://github.com/websanova/js-url#url |
||||
|
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 |
|||||||||||||
|
I believe this to be an accurate and concise way to achieve this (modified from http://css-tricks.com/snippets/javascript/get-url-variables/):
|
|||||||||
|
There's a robust implementation in Node.js's source Also TJ's qs does nested params parsing |
||||
|
|
|||||
|
There are many solutions to retrieve URI query values, I prefer this one because it's short and works great:
|
|||||
|
Most pretty but basic:
It doesn't handle values lists such as |
|||||||||||||
|
If you want array-style parameters URL.js supports arbitrarily nested array-style parameters as well as string indexes (maps). It also handles URL decoding.
|
||||
|
I used this code (JavaScript) to get the what is passed through the URL:
Then to assign the value to a variable, you only have to specify which parameter you want to get, ie if the URL is You can do this to get the values:
then the values would be:
|
|||||
|
The following code will create an object which has two methods:
|
||||
|
I did a small URL library for my needs here: https://github.com/Mikhus/jsurl It's a more common way of manipulating the URLs in JavaScript. Meanwhile it's really lightweight (minified and gzipped < 1 KB) and has a very simple and clean API. And it does not need any other library to work. Regarding the initial question, it's very simple to do:
|
|||||||||
|
If you do not wish to use a JavaScript library you can use the JavaScript string functions to parse
|
||||
|
This is very simple method to get parameter value(query string) Use
|
||||
|
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:
|
||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?