Skip to content

Instantly share code, notes, and snippets.

@mpecka
Last active October 16, 2022 11:38
Show Gist options
  • Select an option

  • Save mpecka/a5ffa84a718802d567664cb12b29cbd2 to your computer and use it in GitHub Desktop.

Select an option

Save mpecka/a5ffa84a718802d567664cb12b29cbd2 to your computer and use it in GitHub Desktop.
Parse location.search query parameters (convert to object)
function getQueryParams(url) {
const paramArr = url.slice(url.indexOf('?') + 1).split('&');
const params = {};
paramArr.map(param => {
const [key, val] = param.split('=');
params[key] = decodeURIComponent(val);
})
return params;
}
getQueryParams('https://example.com/search?q=node&page=2')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment