Created
June 4, 2013 16:23
-
-
Save cfg/5707318 to your computer and use it in GitHub Desktop.
crude hack to retrieve query params using javascript. Should be using a closure, and need to have more intelligent and fully working (doesn't work properly with urlencoded names) array handling.
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
function getParam(p) { | |
if( typeof getParam.qs == 'undefined' ) { | |
getParam.qs = ''; | |
getParam.params = {}; | |
} | |
if( !window.location.search ) { | |
return false; | |
} | |
if( getParam.qs != window.location.search ) { | |
getParam.qs = window.location.search; | |
getParam.params = getAllParams(getParam.qs); | |
} | |
if( !arguments.length || typeof p == 'undefined' ) { | |
return getParam.params; | |
} else if( p in getParam.params ) { | |
return getParam.params[p]; | |
} else { | |
return false; | |
} | |
} | |
function getAllParams() { | |
var e, | |
d = function (s) { return decodeURIComponent(s).replace(/\+/g, " "); }, | |
q = window.location.search.substring(1), | |
r = /([^&=]+)=?([^&]*)/g, | |
urlParams = {}; | |
while (e = r.exec(q)) { | |
if (e[1].indexOf("[") == "-1") | |
urlParams[d(e[1])] = d(e[2]); | |
else { | |
var b1 = e[1].indexOf("["), | |
aN = e[1].slice(b1+1, e[1].indexOf("]", b1)), | |
pN = d(e[1].slice(0, b1)); | |
if (typeof urlParams[pN] != "object") | |
urlParams[d(pN)] = {}, | |
urlParams[d(pN)].length = 0; | |
if (aN) | |
urlParams[d(pN)][d(aN)] = d(e[2]); | |
else | |
Array.prototype.push.call(urlParams[d(pN)], d(e[2])); | |
} | |
} | |
return urlParams; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment