Skip to content

Instantly share code, notes, and snippets.

@milkamil93
Last active November 28, 2019 08:11
Show Gist options
  • Save milkamil93/186189849dd3d71d367c01bed1d2c397 to your computer and use it in GitHub Desktop.
Save milkamil93/186189849dd3d71d367c01bed1d2c397 to your computer and use it in GitHub Desktop.
export default function locationParams(specific = null, value = null) {
let result = {},
loc = location.search.length ? location.search.slice(1).split('&') : null;
if (loc) {
loc.forEach(function (item) {
item = item.split('=');
if (item.length > 1 && item[0] && item[1]) result[item[0]] = item[1];
});
result = !value && specific && specific in result ? result[specific] : result;
}
if (typeof result === 'object' && !Object.keys(result).length || typeof result === 'string' && !result.length) {
result = {};
}
if (value) {
let string = '?';
result[specific] = value;
Object.keys(result).forEach(function (key) {
string += key+'='+result[key]+'&';
});
result = string.slice(0, -1);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment