日期:2014-05-16 浏览次数:20772 次
<!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>
------解决方案--------------------
<!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 = /f