Created
January 6, 2015 21:41
-
-
Save geraldfullam/d262228eab816e508ef6 to your computer and use it in GitHub Desktop.
JavaScript: Global access to query string variables as a map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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