Last active
August 18, 2020 13:34
-
-
Save donbrae/b3cb8200c69fccabede9dbc2793e4ba2 to your computer and use it in GitHub Desktop.
Gets query string and returns as an object of key-value pairs
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
/** | |
* 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