Created
August 9, 2016 23:52
-
-
Save unyo/d58e4651a70c6c8d5b6054309b2bc139 to your computer and use it in GitHub Desktop.
Params.js - A simple, flexible parameter module
This file contains 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
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