内容:function geoip_country_code() { return 'CN'; }
function geoip_country_name() { return 'China'; }
function geoip_city() { return 'Wuhan'; }
function geoip_region() { return '12'; }
function geoip_region_name() { return 'Hubei'; }
function geoip_latitude() { return '30.5801'; }
function geoip_longitude() { return '114.2734'; }
function geoip_postal_code() { return ''; }
function geoip_area_code() { return ''; }
function geoip_metro_code() { return ''; }通过函数名,得到返回值:
如: geoip_country_code 则得到 CN
geoip_country_name 则得到 China
请帮我看一下:
http://topic.csdn.net/u/20120412/16/9254b551-a3fd-4e94-9627-0f3d9945a1af.html
没办法啊,需求是这样的!
你帮我看一下原始的需求:http://topic.csdn.net/u/20120412/16/9254b551-a3fd-4e94-9627-0f3d9945a1af.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="gb2312" />
<title></title>
<style>
</style>
</head>
<body>
<div id="test">
function geoip_country_code() { return 'CN'; }
function geoip_country_name() { return 'China'; }
function geoip_city() { return 'Wuhan'; }
function geoip_region() { return '12'; }
function geoip_region_name() { return 'Hubei'; }
function geoip_latitude() { return '30.5801'; }
function geoip_longitude() { return '114.2734'; }
function geoip_postal_code() { return ''; }
function geoip_area_code() { return ''; }
function geoip_metro_code() { return ''; }
</div>
<script>
function $(el){
return typeof el == 'string' ? document.getElementById(el) : el;
}
var str = $('test').innerHTML;
var json = {};
var re = /function\s(.+?)\(\).+return\s+'(.*?)'.+?}/gi;
str = str.replace(re, function($){
re.test($);
json[RegExp.$1] = RegExp.$2;
});
//console.log(json);
alert(json.geoip_country_code)
alert(json.geoip_country_name)
// 依次
</script>
</body>
</html>
要不然你用if else 或者switch也行
string returnvalue="";
switch(functionname)
{
case "geoip_country_code": returnvalue="CN";break;
................
}
类似这样
<!DOCTYPE HTML>
<html>
<head>
<meta charset="gb2312" />
<title></title>
<style>
</style>
</head>
<body>
<div id="test">
function geoip_country_code() { return 'CN'; }
function geoip_country_name() { return 'China'; }
function geoip_city() { return 'Wuhan'; }
function geoip_region() { return '12'; }
function geoip_region_name() { return 'Hubei'; }
function geoip_latitude() { return '30.5801'; }
function geoip_longitude() { return '114.2734'; }
function geoip_postal_code() { return ''; }
function geoip_area_code() { return ''; }
function geoip_metro_code() { return ''; }
</div>
<script>
function $(el){
return typeof el == 'string' ? document.getElementById(el) : el;
}
var str = $('test').innerHTML;
var json = {};
var re = /function\s(.+?)\(\).+?return\s+?'(.*?)'.+?}/gi;
str = str.replace(re, function($){
re.test($);
var a = RegExp.$1
re.lastIndex = 0;
json[a] = RegExp.$2;
});
//console.log(json);
alert(json.geoip_country_code)
alert(json.geoip_country_name)
alert(json.geoip_area_code)
// 依次
</script>
</body>
</html>修改了一下。
提供几个ip地址API腾讯的IP地址API接口地址:http://fw.qq.com/ipaddress (这个不能用了,以前一直用这个)新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js新浪另外一个IP地址查询接口:(现在一直用这个)
http://counter.sina.com.cn/ip
// var ILData = new Array("125.69.90.148","中国", "四川省", "成都市", "电信"); if (typeof(ILData_callback) != "undefined") { ILData_callback(); }
新浪多地域测试方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.192.3.42搜狐IP地址查询接口(默认GBK):http://pv.sohu.com/cityjson
// var returnCitySN = {"cip": "118.113.51.216", "cid": "510100", "cname": "四川省成都市"};
搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/cityjson?ie=utf-8
搜狐另外的IP地址查询接口:http://txt.go.sohu.com/ip/soip
*/
<input id=functionname>
<input type="button" value="getreturnvalue;" onclick="alert(getreturnvalue(document.getElementById('functionname').value))" />
<br><textarea id=textarea1 rows=3 cols=80></textarea>
function getreturnvalue(name) {
var re=new RegExp("function\\s+"+name+"\\(\\).*return\\s+'(.*)'");
re.global=true;
re.ignorecase = true;
re.multiline = true;
if ((arr = re.exec(textarea1.value)) != null) {
return arr[1];
} else {
return "notfound"
}
}
大哥,你这个蛮好的,但是我这边有两个状况:
1:网站在国外使用
2:返回的结果最好是xml格式的