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 |
|||||||||||||||||
|
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. |
|||||||||
|
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:
|
||||
|
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 |
||||
|
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 |
|||||||||
|
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
|
||||
|
And this is how you can use this function assuming the URL is
|
||||
|
From the MDN:
|
||||
|
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
|
|||||||||
|
I believe this to be an accurate and concise way to achieve this (modified from http://css-tricks.com/snippets/javascript/get-url-variables/):
|
||||
|
A very lightweight jquery method:
And to alert ,for example ?q
|
|||||
|
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.
|
||||
|
The problem with top answer on that question is that it's not support params placed after #, but sometimes it's needed to get this value also. I modify the unswer to let it parse full query string with hash sign also
|
|||||||||
|
I did small URL library for my needs here: https://github.com/Mikhus/jsurl It's more common way of manipulating the URLs in JavaScript, meanwhile it's really lightweight (minified and gzipped < 1KB) and has very simple and clean API. And it does not need any other library to work. Regarding the initial question, it's very simply to do:
|
||||
|
If you are using Browserify you can use the
Further reading: http://nodejs.org/api/url.html |
|||||
|
If you do not wish to use a Javascript library you can use the Javascript string functions to parse window.location. Keep this code in an external .js file and you can use it over and over again in different projects.
|
||||
|
see this post or use this
|
||||
|
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 var queryObj = {}; before document.ready and put the bellow code in the beginning of document.ready. After this code you can use queryObj["queryObjectName"] for any query object you have
|
||||
|
Also Refer : |
|||||
|
There's a robust implementation in Node.js's source Also TJ's qs does nested params parsing |
||||
|
|
||||
|
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:
Thanks, Josh |
||||
|
There are many solutions to retrieve URI query values, I prefer this one because it's short and works great:
|
||||
|
This is very simple method to get parameter value(query string) Use gV(para_name) function to retrieve its value
|
||||
|
We've just released arg.js, a project aimed at solving this problem once and for all. It's traditionally been so difficult but now you can do:
or getting the whole lot:
and if you care about the difference between |
||||
|
Not to beat a dead horse, but if you have underscore or lodash, a quick and dirty way to get this done is:
|
|||||
|
tl;drA quick, complete solution, which handles multivalued keys and encoded characters.
example:
... Read more... about the vanilla JavaScript solutionTo access different parts of url use easiest (dummy) solution
multi-valued keysSimple key check
encoded characters?Enclose the
|
|||||||||||||||||||||
|
Doing this reliably is more involved than one may think at first.
To solve this, here is a configurable API with a healthy dose of defensive programming. Note that it can be made half the size if you are willing to hardcode some of the variables, or if the input can never include Version 1: Returns a data object with names and values for each parameter. It effectively de-duplicates them and always respects the first one found from left-to-right.
Version 2: Returns a data map object with two identical length arrays, one for names and one for values, with an index for each parameter. This one supports duplicate names and intentionally does not de-duplicate them, because that is probably why you would want to use this format.
|
|||||
|
This will parse variables AND arrays from a URL string. It uses neither regex or any external library.
Example:
Output:
|
||||
|
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?