Last active
January 13, 2016 12:26
-
-
Save kaspereden/25b435ce9e175b73fa68 to your computer and use it in GitHub Desktop.
Get url parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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