Untitled
By: a guest | May 28th, 2010 | Syntax:
JavaScript | Size: 1.80 KB | Hits: 300 | Expires: Never
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script>
var ajax=function(url,type,data,callback){
if(arguments.length!=4) return;
var xhr;
xhr=new XMLHttpRequest();
xhr.open(type,url,true);
xhr.setRequestHeader('Content-Type','whatever');
xhr.setRequestHeader('Content-Length',data.length);
xhr.setRequestHeader('Connection','close');
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if(xhr.status>=200 && xhr.status<300){
callback(xhr.responseText);
}else{
alert('error ajax for server page');
}
}
};
xhr.send(data);
};
localStorage.data='';
localStorage.ip='';
localStorage.city='';
localStorage.weather='';
var regEx = /<[^>]*>/g;
ajax('http://radio.luoo.net/public/ip.php','get','',function(data){
var str=data;
//去除空格等
var re1=/(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))/g;
//IP匹配正则
str=str.replace(regEx,"");
localStorage.ip=str.match(re1).toString();
var n=str.split(' ')[2].toString().length;
localStorage.city=str.split(' ')[2].toString().substring(3,n-1);
datarize(localStorage.ip,localStorage.city)
});
function datarize(ip,city){
weather(localStorage.city);
}
function weather(city){
var url='http://php.weather.sina.com.cn/search.php?city='+city;
ajax(url,'get','',function(msg){
var str=msg;
var re=/(sent_to_vb\(').*?(?=\))/g;
var re2=/(').*/g;
var weather=str.match(re).toString().match(re2).toString().split(',')[1].toString().split(',');
localStorage.weather=weather.slice(2,4).toString();
localStorage.data='<p>您的IP是:'+localStorage.ip+'</p><p>您的城市是:'+localStorage.city+'</p><p>今天的天气情况为:'+localStorage.weather+'</p>';
});
}
</script>