Skip to content

Instantly share code, notes, and snippets.

@triple-j
Last active December 13, 2016 20:39
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 triple-j/eb238061d5be139fb55bea4b551dcd1a to your computer and use it in GitHub Desktop.
Save triple-j/eb238061d5be139fb55bea4b551dcd1a to your computer and use it in GitHub Desktop.
Retrieve URL Parameters <http://stackoverflow.com/a/1099670>
/**
* Retrieve URL Parameters <http://stackoverflow.com/a/1099670>
* @param {string} [qs=window.location.search] - Query string
* @returns {object}
*
* @example
* var query = getQueryParams(document.location.search);
* alert(query.foo);
*/
function getQueryParams(qs) {
qs = qs || window.location.search;
qs = qs.split('+').join(' ');
var params = {},
tokens,
re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
}
return params;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment