Skip to content

Instantly share code, notes, and snippets.

@milkamil93
Last active November 28, 2019 08:11
Show Gist options
  • Save milkamil93/60473e83c14820f9cd84b1d8ed0ea54b to your computer and use it in GitHub Desktop.
Save milkamil93/60473e83c14820f9cd84b1d8ed0ea54b to your computer and use it in GitHub Desktop.
function locationParams(specific, value) {
var 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 (typeof value !== 'undefined') {
var 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