Skip to content

Instantly share code, notes, and snippets.

@jonathanconway
Created October 29, 2010 00:33
Show Gist options
  • Save jonathanconway/652644 to your computer and use it in GitHub Desktop.
Save jonathanconway/652644 to your computer and use it in GitHub Desktop.
Load the current querystring parameters into an associative array, taking into account repeated keys
// By JonathanConway.net
// Based off original code by Andy E. http://stackoverflow.com/questions/901115/get-querystring-values-with-jquery/2880929#2880929
Object.prototype.isArray = function () {
/// <summary>
/// Returns true if this object is an array.
/// </summary>
return !(this.push === undefined);
};
var urlParams = {};
(function () {
var e,
a = /\+/g, // Regex for replacing addition symbol with a space
r = /([^&=]+)=?([^&]*)/g,
d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
q = window.location.search.substring(1),
key,
value;
while (e = r.exec(q)) {
key = d(e[1]);
value = d(e[2]);
if (urlParams[key].isArray !== undefined && typeof urlParams[key] === 'undefined')
urlParams[key] = value;
else
if (urlParams[key].isArray())
urlParams[key].push(value);
else
urlParams[key] = [urlParams[key], value];
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment