I'm noob to JavaScript and开发者_StackOverflow社区 want to use Prototype JS Framework to get some URL parameters. Imagine I have the following URL on my current browser:
http://www.somewhere.com?param=abc
how can I get the value of 'param'
using any function or utility of Prototype JS ?
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
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;
精彩评论