Skip to content

Instantly share code, notes, and snippets.

@geraldfullam
Created January 6, 2015 21:41
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 geraldfullam/d262228eab816e508ef6 to your computer and use it in GitHub Desktop.
Save geraldfullam/d262228eab816e508ef6 to your computer and use it in GitHub Desktop.
JavaScript: Global access to query string variables as a map
// Source: http://stackoverflow.com/a/13455920/2502532
// -------------------------------------------------------------------
// Add prototype for 'window.location.query([source])' which contain an object
// of querystring keys and their values
// -------------------------------------------------------------------
if(!window.location.query) {
window.location.query = function(source){
var map = {};
source = source || this.search;
if ("" != source) {
var groups = source.substr(1).split("&"), i;
for (i in groups) {
i = groups[i].split("=");
map[decodeURIComponent(i[0])] = decodeURIComponent(i[1]);
}
}
return map;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment