Skip to content

Instantly share code, notes, and snippets.

@dashedstripes
Created May 31, 2017 10:52
Show Gist options
  • Save dashedstripes/fcf19367b4feeecca1399aa68b3db7ad to your computer and use it in GitHub Desktop.
Save dashedstripes/fcf19367b4feeecca1399aa68b3db7ad to your computer and use it in GitHub Desktop.
Create an object from query params on a url
/**
* Returns the query params of a url
* @param {string} url - the url to process
* @return {object} a key value object of the params
*/
function getURLParams(url) {
let params = {};
let rawParams = '';
if(url.split('?')[1] != undefined) {
rawParams = url.replace('%20', ' ').split('?')[1];
}
if(rawParams != '') {
let paramsArray = rawParams.split('&');
paramsArray.forEach((val, index) => {
let item = val.split('=');
params[item[0]] = item[1]
})
}
return params
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment