码迷,mamicode.com
首页 > Web开发 > 详细

js根据IP取得天气

时间:2014-07-01 18:28:37      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:blog   http   java   os   javascript   html   

<span id="weather"></span>
<script>
function weather(cityName) {
    var charset  = (document.charset || document.characterSet || ‘utf-8‘).toLowerCase() == ‘utf-8‘ ? ‘utf-8‘ : ‘gb2312‘;
    var cityName = cityName ? cityName : ‘‘;
    var oScript  = document.createElement(‘script‘);
    oScript.type = "text/javascript"; oScript.async = true;
    oScript.src  = ‘http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&day=0&city=‘+ cityName +‘&dfc=1&charset=‘+ charset;
    (document.getElementsByTagName(‘head‘)[0] || document.getElementsByTagName(‘body‘)[0]).appendChild(oScript);
    var task     = setInterval(function(){
        if(window.SWther.w != ‘undefined‘) {
            clearInterval(task);
            var city = ‘‘, weatherData = ‘‘, dataInfo = ‘‘;
            for(city in window.SWther.w){
                dataInfo = SWther.w[city][0];
                weatherData = {
                        city : city ,
                        date : SWther.add.now.split(‘ ‘)[0] || ‘‘,
                        day_weather: dataInfo.s1,
                        night_weather :dataInfo.s2,
                        day_temp: dataInfo.t1,
                        night_temp: dataInfo.t2,
                        day_wind:dataInfo.p1,
                        night_wind: dataInfo.p2
                    };
            }
            var w = weatherData.city + ‘ 白天:‘+ weatherData.day_weather + ‘ /‘+ weatherData.day_temp + ‘°C‘ +
                    ‘ 夜晚:‘+ weatherData.night_weather + ‘ /‘+ weatherData.night_temp + ‘°C‘;
            document.getElementById(‘weather‘).innerHTML = w;
        }
    }, 1000);
}
weather();
</script>

 

js根据IP取得天气,布布扣,bubuko.com

js根据IP取得天气

标签:blog   http   java   os   javascript   html   

原文地址:http://www.cnblogs.com/lifelog/p/3817842.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!