Possible Duplicate:
Use the get paramater of the url in javascript
How can I get query string values in JavaScript?
In Javascript, how can I get the parameters of a URL string (not the current URL)?
like:
www.domain.com/?v=123&p=hello
Can I get "v" and "p" in a JSON object?
Today (2.5 years after this answer) you can safely use
Array.forEach
. As @ricosrealm suggests,decodeURIComponent
was used in this function.actually it's not that simple, see the peer-review in the comments, especially:
Maybe this should go to codereview SE, but here is safer and regexp-free code:
I also replaced non-encoded
+
for space according to this article which is also useful guide how to encode adhering to RFC 3986.Note the
result[key][index] = val
: a new array item is created, it is enumerable, so it can be iterated byforEach
call. Therefore, you can parse even URLs likenow v and p are objects which have 123 and hello in them respectively
You could get a JavaScript object containing the parameters with something like this:
The regular expression could quite likely be improved. It simply looks for name-value pairs, separated by
=
characters, and pairs themselves separated by&
characters (or an=
character for the first one). For your example, the above would result in:{v: "123", p: "hello"}
Here's a working example.