首页 > 网站 > WEB开发 > 正文

js根据IP取得天气

2024-04-27 14:22:03
字体:
来源:转载
供稿:网友

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>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表