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>

 

你可能感兴趣的:(js)