Skip to content

Instantly share code, notes, and snippets.

@unyo
Created August 9, 2016 23:52
Show Gist options
  • Save unyo/d58e4651a70c6c8d5b6054309b2bc139 to your computer and use it in GitHub Desktop.
Save unyo/d58e4651a70c6c8d5b6054309b2bc139 to your computer and use it in GitHub Desktop.
Params.js - A simple, flexible parameter module
var params = function(w) {
var win = w || window;
var baseQstring = (win.location.search) ? win.location.search : (win.location.hash.split('?')[1] || '');
var qstring = decodeURIComponent(baseQstring.replace('?',''));
var keyval_groups = qstring.split(/[\?&]/);
var p = {};
for (var i in keyval_groups) {
var first_equal = keyval_groups[i].indexOf('=')
var key = keyval_groups[i].slice(0, first_equal);
var value = keyval_groups[i].slice(first_equal+1);
// convert "true" and "false" to boolean
if (value === "true") {
value = true;
}
else if (value === "false") {
value = false;
}
if (key.length > 1) p[key] = value;
}
return p;
};
var internalParams = params(window);
var externalParams = params(window.top);
var mergedParams = Object.assign(externalParams, internalParams);
module.exports = mergedParams;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment