Skip to content

Instantly share code, notes, and snippets.

@robsonsobral
Last active January 4, 2019 18:48
Show Gist options
  • Save robsonsobral/73b41a4ced0ab4e44ac85ccc6f8b542b to your computer and use it in GitHub Desktop.
Save robsonsobral/73b41a4ced0ab4e44ac85ccc6f8b542b to your computer and use it in GitHub Desktop.
function getQueryString() {
var url = arguments.length > 0 && arguments[0] !== undefined ?
arguments[0] :
window.location.search;
var queryString = url.indexOf('?') !== -1 ?
url.split('?')[1] :
url;
var queries = queryString.indexOf('&') !== -1 ?
queryString.split('&') :
[queryString];
var params = {};
for (let i = 0; i < queries.length; i += 1) {
var element = queries[i].indexOf('=') !== -1 ?
queries[i].split('=') :
[queries[i], null];
params[decodeURIComponent(element[0])] = decodeURIComponent(element[1] || '');
}
return params;
}
function setQueryString(params) {
return '?' + Object
.keys(params)
.map(function (key) {
return key + '=' + encodeURIComponent(params[key])
})
.join('&');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment