Skip to content

Instantly share code, notes, and snippets.

@JacobBennett
Created February 1, 2017 02:26
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 JacobBennett/a577409c3ffccb7e54e2f6d1888ec006 to your computer and use it in GitHub Desktop.
Save JacobBennett/a577409c3ffccb7e54e2f6d1888ec006 to your computer and use it in GitHub Desktop.
Parsing Get Parameters in JS
/*
* Params.js
*
* http://lecklider.com/2015/12/parsing-get-parameters-in-javascript.html
*
* Super simple parser of query string parameters.
* Creates a variable 'params' on the 'window' object.
*
*/
(function() {
var params = {},
capture = void 0,
query = window.location.search.substring(1),
whitespace = /\+/g,
regex = /([^&=]+)=?([^&]*)/g,
decode = function(s) {
return decodeURIComponent(s.replace(whitespace, " "));
};
while (capture = regex.exec(query)) {
var key = decode(capture[1]),
value = decode(capture[2]);
if (value !== '') {
params[key] = value;
}
}
this.params = params;
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment