-
-
Save santaklouse/b6fb1ff671869a597586841993058d4b to your computer and use it in GitHub Desktop.
Parse URL query parameters in ES6
This file contains hidden or 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
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