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 |
|||||||||||||||||||||
|
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.
|
||||
|
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
|
|||||||||
|
This will parse variables AND arrays from a URL string. It uses neither regex or any external library.
Example:
Output:
|
||||
|
this will work.. You need call this function where you need get the parameter by passing its name..
|
|||||
|
The shortest possible expression in terms of size to obtain a query object seems to be:
You can make use of the
|
||||
|
quick, easy, and fast: The Function:
Usage:
|
|||||||||
|
Here is String prototype implementation:
Example call:
|
|||||||||
|
see this post or use this
|
|||||||||
|
Simple Solution with Plain JS and Regex
|
||||
|
This didn't work for me, I want to match `?b' as the 'b' parameter is present, and not match '?return' as the 'r' parameter, here is my solution.
|
|||||||||
|
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 function will return a parsed JavaScript object with any arbitrarily nested values using recursion as necessary. Here's a jsfiddle example.
Given any of the above test examples.
|
|||||||||
|
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?