Skip to content

Instantly share code, notes, and snippets.

@santaklouse
Forked from pirate/parseURLParameters.js
Last active February 9, 2021 16:56
Show Gist options
  • Save santaklouse/b6fb1ff671869a597586841993058d4b to your computer and use it in GitHub Desktop.
Save santaklouse/b6fb1ff671869a597586841993058d4b to your computer and use it in GitHub Desktop.
Parse URL query parameters in ES6
const getUrlParams = search => {
let params = {};
search
.slice(search.indexOf('?') + 1)
.split('&')
.forEach(hash => {
const [key, val] = hash.split('=')
params[key] = val === void(0)
? true
: decodeURIComponent(val);
});
return params;
};
// window.location.hash
const hash = '#/requests/page/itemz?live_only&srchopen&pagenum=1&pagesize=25';
console.log(getUrlParams(hash));
// Result
// > {live_only: true, srchopen: true, pagenum: "1", pagesize: "25"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment