I found an easy method to get url parameters with JavaScript. The beauty is that it uses a JavaScript closure to cache the values so that it can be called multiple times without unneeded processing:
var urlParameter = (function() {
var parameters = undefined;
return function (paramName) {
if (parameters == undefined) {
parameters = {};
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
parameters[hash[0]] = hash[1];
}
}
return parameters[paramName];
}
}());
Usage:
urlParameter(parameterName);
Advertisement