I know you can obtain an URL variable by calling getUrlVars()["id"]
, however is there a way to get all (an unknown number of) variables in the URL? For a few reasons I am only allowed to do this on client side.
Asked
Active
Viewed 6,317 times
4

Pupper
- 2,315
- 2
- 22
- 29
-
1See the second answer (high-voted) http://stackoverflow.com/questions/647259/javascript-query-string – Michael Berkowski Jan 13 '12 at 04:05
-
Cheers Michael, location.search.substring(1) works great. – Pupper Jan 13 '12 at 04:38
2 Answers
6
try this:
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars[hash[0]] = hash[1];
}
return vars;
}
var url_vars = getUrlVars();
for(var i in url_vars)
{
alert(i + " == " + url_vars[i]);
}

Dave Lasley
- 5,262
- 1
- 34
- 37

redmoon7777
- 4,498
- 1
- 24
- 26