Accessing the QueryString in JavaScript
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){Figure 1
var querystring = {};
location.search.replace(/([^?=&]+)(=([^&]*))?/g, function($0, $1, $2, $3){
querystring[$1] = $3;
});
location.querystring = querystring;
})(window.location);
Comments