Guest
Public paste!

Untitled

By: a guest | May 28th, 2010 | Syntax: JavaScript | Size: 1.80 KB | Hits: 300 | Expires: Never
Copy text to clipboard
  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  2. <script>
  3. var ajax=function(url,type,data,callback){
  4.         if(arguments.length!=4) return;
  5.         var xhr;
  6.         xhr=new XMLHttpRequest();
  7.         xhr.open(type,url,true);
  8.         xhr.setRequestHeader('Content-Type','whatever');
  9.         xhr.setRequestHeader('Content-Length',data.length);
  10.         xhr.setRequestHeader('Connection','close');
  11.         xhr.onreadystatechange=function(){
  12.           if(xhr.readyState==4){
  13.                 if(xhr.status>=200 && xhr.status<300){
  14.                  callback(xhr.responseText);
  15.                 }else{
  16.                  alert('error ajax for server page');
  17.                 }
  18.           }
  19.         };
  20.         xhr.send(data);
  21.   };
  22.  
  23.   localStorage.data='';
  24.   localStorage.ip='';
  25.   localStorage.city='';
  26.   localStorage.weather='';
  27.   var regEx = /<[^>]*>/g;
  28.  
  29.  ajax('http://radio.luoo.net/public/ip.php','get','',function(data){
  30.         var str=data;
  31.        
  32.         //去除空格等
  33.         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;
  34.         //IP匹配正则
  35.         str=str.replace(regEx,"");
  36.        
  37.         localStorage.ip=str.match(re1).toString();
  38.                
  39.         var n=str.split(' ')[2].toString().length;
  40.         localStorage.city=str.split(' ')[2].toString().substring(3,n-1);
  41.        
  42.         datarize(localStorage.ip,localStorage.city)
  43.  });
  44.  function datarize(ip,city){
  45.        
  46.         weather(localStorage.city);
  47.        
  48.        
  49.  }
  50.  function weather(city){
  51.         var url='http://php.weather.sina.com.cn/search.php?city='+city;
  52.          ajax(url,'get','',function(msg){
  53.                 var str=msg;
  54.                 var re=/(sent_to_vb\(').*?(?=\))/g;
  55.                 var re2=/(').*/g;
  56.                 var weather=str.match(re).toString().match(re2).toString().split(',')[1].toString().split(',');
  57.                 localStorage.weather=weather.slice(2,4).toString();
  58.                 localStorage.data='<p>您的IP是:'+localStorage.ip+'</p><p>您的城市是:'+localStorage.city+'</p><p>今天的天气情况为:'+localStorage.weather+'</p>';
  59.          });
  60.  }
  61. </script>