Created
August 26, 2008 14:30
-
-
Save beastaugh/7269 to your computer and use it in GitHub Desktop.
Turn a document query string into a JavaScript object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Parse an HTTP query string into a JS object. | |
*/ | |
var parseQueryString = function(query) { | |
var uri = query || document.location.href, | |
params = {}, | |
args, i, pair, key, value; | |
if (!uri) return params; | |
args = uri.substring(uri.indexOf('?') + 1, uri.length).split('&'); | |
i = args.length; | |
if (i < 1) return params; | |
while(i--) { | |
pair = args[i].split('='); | |
key = pair[0]; | |
value = pair[1]; | |
if (key && value) { | |
params[key] = value; | |
} | |
} | |
return params; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment