Skip to content

Instantly share code, notes, and snippets.

@Rycochet
Last active June 16, 2016 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rycochet/01e4e152bb2538e1c6a0bc555aa44352 to your computer and use it in GitHub Desktop.
Save Rycochet/01e4e152bb2538e1c6a0bc555aa44352 to your computer and use it in GitHub Desktop.
Parse (and cache) the query string and return the requested search value
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