Skip to content

Instantly share code, notes, and snippets.

@robcolburn
Created May 3, 2012 20:11
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 robcolburn/2588921 to your computer and use it in GitHub Desktop.
Save robcolburn/2588921 to your computer and use it in GitHub Desktop.
Parse query string parameters
function getQuery(s) {
var query = {}, i = s.indexOf('?');
if (i != -1) {
s.substr(i+1).replace(/\+/g, ' ').replace(/([^&;=]+)=?([^&;]*)/g, function (m, k, v) {
query[decodeURIComponent(k)] = decodeURIComponent(v);
});
}
return query;
}
//_GET = getQuery(location.href);
@robcolburn
Copy link
Author

Thanks! Nice to know people can use this bits I post.

I know where you're coming from, there are often good cases for wanting to pass lists over a query string. I like to think of query-strings as simple key-value maps, so only 1 value per key.

PHP's $_GET and jQuery's $.param also allow an alternative bracket-like syntax to be used for array's, as in ?a[]=1&b=2&a[]=3. I could see supporting that, as it still allows the key-value relationship.

IMO, I like the functions simplicity and map relationship.

If you're looking for something more robust, I recommend @cowboy's:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment