Last active
August 13, 2017 09:50
-
-
Save ladas-larry/fffc9391de3b5567661c64f3937060ce to your computer and use it in GitHub Desktop.
Parse query string
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
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