0 Comments

Yesterday I needed to access a querystring parameter, from JavaScript. There is no in-built object or property but there are plenty of examples of how to do this. However, I've written a neat self executing function (Figure 1) that gets the key/value pairs, for each querystring parameter, and creates a querystring property on the window.location object.

(function(location){
var querystring = {};
  location.search.replace(/([^?=&]+)(=([^&]*))?/g, function($0, $1, $2, $3){
querystring[$1] = $3;
});
location.querystring = querystring;
})(window.location);
Figure 1

Comments