Skip to content

Instantly share code, notes, and snippets.

@kaspereden
Last active January 13, 2016 12:26
Show Gist options
  • Save kaspereden/25b435ce9e175b73fa68 to your computer and use it in GitHub Desktop.
Save kaspereden/25b435ce9e175b73fa68 to your computer and use it in GitHub Desktop.
Get url parameters
function get(key) {
var searchParams = {},
search = window.location.search.substring(1); // substring to remove the ?
if (search) {
search = search.split('&');
for (var i in search) {
if (search.hasOwnProperty(i) && i !== '') {
var pair = search[i].split('=');
pair[0] = decodeURIComponent(pair[0]);
pair[1] = decodeURIComponent(pair[1]);
searchParams[pair[0]] = pair[1];
}
}
}
if (key) {
return searchParams[key];
}
return searchParams;
}
function getUrlParameters() {
var values = window.location.search.replace('?', '').split('&'),
urlParameters = {},
valLen = values.length;
for (var i = 0; i < valLen; i = i + 1) {
var pair = values[i].split('=');
if (decodeURIComponent(pair[0]).indexOf('[]') !== -1) {
var key = decodeURIComponent(pair[0]);
key = key.replace('[]', '');
if (!urlParameters[key]) {
urlParameters[key] = [];
}
urlParameters[key].push(decodeURIComponent(pair[1]));
} else {
// Normal value.
urlParameters[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
}
return urlParameters;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment