Last active
December 15, 2023 07:17
-
-
Save pirate/9298155edda679510723 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
function getUrlParams(search) { | |
const hashes = search.slice(search.indexOf('?') + 1).split('&') | |
const params = {} | |
hashes.map(hash => { | |
const [key, val] = hash.split('=') | |
params[key] = decodeURIComponent(val) | |
}) | |
return params | |
} | |
console.log(getUrlParams(window.location.search)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unfortunately even the native
URLSearchParams
implementation doesn't handle array query parameters. :(So, here's another take on array query parameters. Tested on