Possible Duplicate:
How can I get query string values?
I need to parse the query string www.mysite.com/default.aspx?dest=aboutus.aspx
.
How do I get the dest
variable in JavaScript?
I need to parse the query string |
|||||||
|
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Here is a fast and easy way of parsing query strings in JavaScript:
Now make a request to page.html?x=Hello:
|
|||||||||||||||
|
You can also use the excellent URI.js library by Rodney Rehm. Here's how:-
And to parse the query string of current page:-
|
|||||
|
Here's my version based loosely on Braceyard's version above but parsing into a 'dictionary' and support for search args without '='. In use it in my JQuery $(document).ready() function. The arguments are stored as key/value pairs in argsParsed, which you might want to save somewhere...
|
|||||||||
|
If you know that you will only have that one querystring variable you can simply do:
|
|||||||||
|
|
|||||||||||
|
Have a look at this solution. Using his function, you would just not to call |
|||
|
The following function will parse the search string with a regular expression, cache the result and return the value of the requested variable:
}; You can either call it once without any parameters and work with the |
|||||||
|
How about this?
..then just call it with:
Cheers |
||||
|
I wanted to pick up specific links within a DOM element on a page, send those users to a redirect page on a timer and then pass them onto the original clicked URL. This is how I did it using regular javascript incorporating one of the methods above. Page with links: Head
Body
Redirect page Head
Body
|
|||||
|
Following on from my comment to the answer @bobby posted, here is the code I would use:
This code takes in the querystring provided (as 'str') and returns an object. The string is split on all occurances of &, resulting in an array. the array is then travsersed and each item in it is split by "=". This results in sub arrays wherein the 0th element is the parameter and the 1st element is the value (or undefined if no = sign). These are mapped to object properties, so for example the string "hello=1&another=2&something" is turned into:
In addition, this code notices repeating reoccurances such as "hello=1&hello=2" and converts the result into an array, eg:
You'll also notice it deals with cases in whih the = sign is not used. It also ignores if there is an equal sign straight after an & symbol. A bit overkill for the original question, but a reusable solution if you ever need to work with querystrings in javascript :) |
||||
|
I wanted a simple function that took a URL as an input and returned a map of the query params. If I were to improve this function, I would support the standard for array data in the URL, and or nested variables. This should work back and for with the jQuery.param( qparams ) function.
|
|||
|
Me too! http://jsfiddle.net/drzaus/8EE8k/ (Note: without fancy nested or duplicate checking)
And tests:
Results in:
|
|||
|