js根据IP取得天气

<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>

 

转载于:https://www.cnblogs.com/lifelog/p/3817842.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值