Skip to content

Instantly share code, notes, and snippets.

@maxvipon
Created April 23, 2015 07:33
Show Gist options
  • Save maxvipon/780f7a94d2bbb006bb42 to your computer and use it in GitHub Desktop.
Save maxvipon/780f7a94d2bbb006bb42 to your computer and use it in GitHub Desktop.
Deserialize URL query params
var args = location.search.substr(1)
.split('&')
.map(function (pair) {
pair = pair.split('=');
try {
pair[1] = pair[1] !== undefined ? decodeURIComponent(pair[1]) : true;
if (typeof pair[1] === 'string') {
pair[1] = pair[1].replace(/\+/g, ' ');
if (pair[1].indexOf(',') !== -1) {
pair[1] = pair[1].split(',');
}
}
} catch (err) {
console.error('Query parameter ' + pair[0] + ' is not correct');
pair = null;
}
return pair;
})
.filter(Boolean)
.reduce(function (prev, next) {
prev[next[0]] = next[1];
return prev;
}, {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment