Skip to content

Instantly share code, notes, and snippets.

@ladas-larry
Last active August 13, 2017 09:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ladas-larry/fffc9391de3b5567661c64f3937060ce to your computer and use it in GitHub Desktop.
Save ladas-larry/fffc9391de3b5567661c64f3937060ce to your computer and use it in GitHub Desktop.
Parse query string
function parseQuery(locationSearch) {
locationSearch = locationSearch.substring(1); // remove "?" at the beginning
var values = locationSearch.split('&');
var queryObj = values.reduce(function (result, item) {
var parts = item.split('=');
result[decodeURIComponent(parts[0])] = parts[1];
return result;
}, {});
return queryObj;
}
const query = window.location.search; // pageSize=24&sort=asc
const queryObj = parseQuery(query); // {pageSize: 24, sort: 'asc'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment