I\'m noob to JavaScript and want to use Prototype JS Framework to get some URL parameters. Imagine I have the following URL on my current browser:
http://www
Expanding on Scott's answer: to put the value of the URL variable 'param' into the javascript variable 'x' you would use Prototype like so:
x = document.URL.toQueryParams().param;
Prototype.js DOES provide a utility:
uri.toQueryParams();
You really don't need Prototype for this:
function get_param(param) {
var search = window.location.search.substring(1);
var compareKeyValuePair = function(pair) {
var key_value = pair.split('=');
var decodedKey = decodeURIComponent(key_value[0]);
var decodedValue = decodeURIComponent(key_value[1]);
if(decodedKey == param) return decodedValue;
return null;
};
var comparisonResult = null;
if(search.indexOf('&') > -1) {
var params = search.split('&');
for(var i = 0; i < params.length; i++) {
comparisonResult = compareKeyValuePair(params[i]);
if(comparisonResult !== null) {
break;
}
}
} else {
comparisonResult = compareKeyValuePair(search);
}
return comparisonResult;
}
var param_value = get_param('param'); //abc