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:
|
|||||||||||||||||||||
|
URLSearchParamsFirefox 44+, Opera 36+ and Chrome 49+ support the URLSearchParams API: Safari Nightly has implemented it as well. It is not standardized by W3C, but it is a living standard by WhatWG. You can use it on location, but you need to remove the
Or of course on any URL:
And you read/set parameters through the There's also a google-suggested URLSearchParams polyfill, a reference implementation, and a sample page if you want to start using this API without relying on latest version of Chrome/Firefox/Opera. |
|||||||||
|
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
|
|||||
|
I would rather use
|
||||
|
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 |
||||
|
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:
|
||||
|
This is very simple method to get parameter value(query string) Use
|
||||
|
If you do not wish to use a JavaScript library you can use the JavaScript string functions to parse
|
||||
|
I recommend Dar Lessons as a good plugin. I have worked with it fo a long time. You can also use the following code.
Jus put
|
|||||||||
|
Here is
Example call:
|
|||||||||
|
This didn't work for me, I want to match
|
|||||||||
|
tl;drA quick, complete solution, which handles multivalued keys and encoded characters.
Multi-lined:
Example:
short-circuit evaluation, ES6 Destructuring assignments, Arrow functions Read more... about the Vanilla JavaScript solution.To access different parts of a URL use Easiest (dummy) solution
Multi-valued keysSimple key check
Encoded characters?Use
Example:
*!!! Please note, that
|
|||||||||||||||||||||
|
Here's what I'm using:
Returning 'override' rather than '0' in the last case makes it consistent with PHP. Works in IE7. |
|||||||||||||||||||||
|
Keep it simple in plain JavaScript code:
Call it from anywhere in the JavaScript code:
|
|||||
|
Here's my edit to this excellent answer - with added ability to parse query strings with keys without values.
IMPORTANT! The parameter for that function in the last line is different. It's just an example of how one can pass an arbitrary URL to it. You can use last line from Bruno's answer to parse the current URL. So what exactly changed? With url |
||||
|
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 a URL like |
||||
|
If you have Underscore.js or lodash, a quick and dirty way to get this done is:
|
|||||||||||||
|
Use:
Please check and let me know your comments. Also refer to How to get querystring value using jQuery. |
|||||||||
|
See this post or use this:
|
|||||||||||||
|
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:
|
|||||||||
|
The problem with the top answer on that question is that it's not-supported parameters placed after #, but sometimes it's needed to get this value also. I modified the answer to let it parse a full query string with a hash sign also:
|
|||||||||||||
|
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.
|
||||
|
A very lightweight jQuery method:
And to alert, for example ?q
|
|||||||||
|
I needed an object from the query string, and I hate lots of code. It may not be the most robust in the universe, but it's just a few lines of code.
A URL like
|
|||||||||||||||||
|
One-liner to get the query:
|
|||||||||||||
|
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:
|
||||
|
Just use two splits:
I was reading all the previous and more complete answers. 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. |
|||||||||||||||||||||
|
Quick, easy, and fast: The function:
Usage:
|
|||||||||
|
A simple solution with plain JavaScript and regular expressions:
|
||||
|
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?