Skip to content

Instantly share code, notes, and snippets.

@typerandom
Created January 14, 2020 13:22
Show Gist options
  • Save typerandom/8c59a7dadc1ed722e465195fac4d1af7 to your computer and use it in GitHub Desktop.
Save typerandom/8c59a7dadc1ed722e465195fac4d1af7 to your computer and use it in GitHub Desktop.
function getQueryParams() {
const queryString = document.location.search;
if (!queryString) {
return {};
}
return (
// Strip the initial ?
queryString.substring(1)
// Split by parameter delimiter
.split('&')
// Split each parameter by key/value delimiter
.map(segment => segment.split('=', 2))
// Reduce to a key/value dictionary
.reduce(
(result, segment) => {
const key = decodeURIComponent(segment[0]);
let value = decodeURIComponent(segment[1]);
// Translate plus to space and write it to the result
value = value.replace(/\+/ig, ' ');
result[key] = value;
return result;
},
{},
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment