Skip to content

Instantly share code, notes, and snippets.

@Leko
Created May 22, 2014 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leko/d24b4b811ea6f592030f to your computer and use it in GitHub Desktop.
Save Leko/d24b4b811ea6f592030f to your computer and use it in GitHub Desktop.
[js] parse GET parameters
function parseQuery(query) {
return query.split("&").reduce(function(memo, query){
var parts = query.split('='),
key = parts[0],
val = encodeURIComponent(parts[1]);
if(key.indexOf('[]') == key.length-2) {
key = key.replace('[]', '');
memo[key] = Array.isArray(memo[key]) ? memo[key] : [];
memo[key].push(val);
} else {
memo[key] = val;
}
return memo;
}, {});
}
// PHP like
$_GET = parseQuery(location.search);
// $_GET['hoge']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment