SimpleWeather.js - 仅在互联网可用时加载

时间:2014-06-18 11:44:45

标签: jquery html css jquery-mobile cordova

我有一个iOS应用程序,它使用SimpleWeather插件收集实际天气信息(包括7天预报)。

现在......雅虎图像服务器已关闭(s.yimg.com),这会导致我的Flexslider脚本失效(无论出于何种原因)。我的头像这样:

    <head>
    <meta name="format-detection" content="telephone=no" />
    <meta charset="utf-8" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1" />
    <link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.4.2.css" />
    <link rel="stylesheet" type="text/css" href="css/stylesheet.css" />
    <script type="text/javascript" src="js/jquery-1.11.1.js"></script>
    <script type="text/javascript" src="js/jquery.mobile-1.4.2.js"></script>
    <script type="text/javascript" src="js/jquery.flexslider.js"></script>
    <script type="text/javascript" src="js/jquery.simpleWeather.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>

<script type="text/javascript" charset="utf-8">
$(window).load(function() {
    $('.flexslider').flexslider();
});
</script>

<script>
$(function() {
    $.mobile.defaultHomeScroll = 0;
});
</script>

<script type="text/javascript">
        $(document).ready(function() {
            $.simpleWeather({
                woeid: '329747',
                unit: 'c',
                success: function(weather) {
                    var sBasicTemp = weather.temp+'&deg;'+weather.units.temp;
                    var sBasicIcon = weather.thumbnail;
                    var weatherForecastAll = '<ul class="weatherList">';
                    var listClass = '';
                    for(var i=0;i<weather.forecast.length;i++) {
                        if(i == (weather.forecast.length-1)){listClass = ' class="last"';}
                        if(i == 0) {
                            weatherForecastDay = 'Heute';
                        } else {
                            var forcastDay = weather.forecast[i].day;
                            switch(forcastDay){
                                case('Mon'):
                                    weatherForecastDay = 'Montag';
                                    break;
                                case('Tue'):
                                    weatherForecastDay = 'Dienstag';
                                    break;
                                case('Wed'):
                                    weatherForecastDay = 'Mittwoch';
                                    break;
                                case('Thu'):
                                    weatherForecastDay = 'Donnerstag';
                                    break;
                                case('Fri'):
                                    weatherForecastDay = 'Freitag';
                                    break;
                                case('Sat'):
                                    weatherForecastDay = 'Samstag';
                                    break;
                                case('Sun'):
                                    weatherForecastDay = 'Sonntag';
                                    break;
                            }
                        }
                        weatherForecastAll += '<li'+listClass+'><div class="clear weatherListCont">'+weatherForecastDay+'</div><div class="weatherListCont"><img src="'+weather.forecast[i].thumbnail+'" alt="" /></div><div class="weatherListCont">'+weather.forecast[i].low+'&deg;'+weather.units.temp+' - '+weather.forecast[i].high+'&deg;'+weather.units.temp+'</div></li>';
                    }
                    weatherForecastAll += '</ul>';

                        $(".weatherBasicIcon").html('<img src="'+sBasicIcon+'" alt="" />');
                        $(".weatherBasicTemp").html(sBasicTemp);
                        $(".weatherBasicTempHead").html('<img width="45px" height="auto" src="'+sBasicIcon+'" alt="" /> '+sBasicTemp);
                        $(".weatherForecastAll").html(weatherForecastAll);
                },
                error: function(error) {
                    $(".weatherBasicTempHead").html('---');
                }
            });
        });
    </script>

我可以说哪个命令只能在互联网可用时执行天气脚本,最好是显示弹出窗口“没有互联网连接 - 天气不可用”。

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用navigator.online:

if (navigator.onLine) {
    // I'm online so submit the form.
}

注意它适用于htmls 5和基于webkit的浏览器:)

Check if Internet Connection Exists with Javascript?