Created
July 1, 2018 15:10
-
-
Save magician11/5914ae687e8494f5b4718263eef7003e to your computer and use it in GitHub Desktop.
How to get all parameters from a URL as an associative array
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
export const getURLparams = () => { | |
const pl = /\+/g; | |
const search = /([^&=]+)=?([^&]*)/g; | |
const decode = s => decodeURIComponent(s.replace(pl, ' ')); | |
const query = window.location.search.substring(1); | |
const urlParams = {}; | |
let match; | |
while ((match = search.exec(query))) { | |
urlParams[decode(match[1])] = decode(match[2]); | |
} | |
return urlParams; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code inspired from https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript/2880929#2880929
usage:
if the URL is https://www.google.com/?bob=john&id=123
this would output