使用Prototype JavaScript从当前URL获取URL参数 [英] get URL Parameters from current URL using Prototype JavaScript
本文介绍了使用Prototype JavaScript从当前URL获取URL参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不喜欢JavaScript,并希望使用 Prototype JS Framework来获取一些URL参数。
想象一下,我当前的浏览器上有以下网址:
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.somewhere.com?param=abc
如何获得'param'的值
使用 Prototype JS 的任何功能或实用程序?
how can I get the value of 'param'
using any function or utility of Prototype JS ?
推荐答案
你真的不需要原型:
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
这篇关于使用Prototype JavaScript从当前URL获取URL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文