Last active
June 16, 2016 09:38
-
-
Save Rycochet/01e4e152bb2538e1c6a0bc555aa44352 to your computer and use it in GitHub Desktop.
Parse (and cache) the query string and return the requested search value
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 getQuery(search) { | |
if (!getQuery.parsed) { | |
var match, key, value, | |
rx = /([^=&]+)(?:=([^&]*))?/g, | |
parsed = getQuery.parsed = {}; | |
while ((match = rx.exec(document.location.search.substring(1)))) { | |
key = decodeURIComponent(match[1]); | |
value = match[2] === undefined ? true : decodeURIComponent(match[2]); | |
if (parsed.hasOwnProperty(key)) { | |
if (!Array.isArray(parsed[key])) { | |
parsed[key] = [parsed[key]]; | |
} | |
parsed[key].push(value); | |
} else { | |
parsed[key] = value; | |
} | |
} | |
} | |
return getQuery.parsed[search]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment