Skip to content

Instantly share code, notes, and snippets.

@ladas-larry ladas-larry/parseQuery.js
Last active Aug 13, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.