Skip to content

Instantly share code, notes, and snippets.

@donbrae
Last active August 18, 2020 13:34
Show Gist options
  • Save donbrae/b3cb8200c69fccabede9dbc2793e4ba2 to your computer and use it in GitHub Desktop.
Save donbrae/b3cb8200c69fccabede9dbc2793e4ba2 to your computer and use it in GitHub Desktop.
Gets query string and returns as an object of key-value pairs
/**
* Gets query string and returns as an object of key-value pairs
* @returns {Object or null}
*/
function getQueryString() {
let q = document.location.href.split('?')[1];
let o = {};
if (q && q.length) {
q = q.split('&');
q.forEach(element => {
if (element.indexOf('=') > -1) { // If the query string parameter has a value
let key_value = element.split('=');
o[key_value[0]] = key_value[1];
} else {``
o[element] = undefined;
}
});
if (Object.keys(o).length)
return o;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment