Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to get all parameters from a URL as an associative array
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;
};
@magician11

This comment has been minimized.

Copy link
Owner Author

commented Jul 1, 2018

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

const params = getURLparams();
console.log(params);
console.log(params['id']);

this would output

Object {bob: "john", id: "123"}
123 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.