在onDeviceReady中,我试图检测navigator.connection.type是否存在网络连接,如果不存在,则显示具有脱机内容的div,但它不起作用。在设备(Z10)上运行它。

我的页面

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="format-detection" content="telephone-no">
    <meta name="viewport" content="user-scalable=no, initial-scale=1, minimum-scale=1, maximum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi">
    <title>Our Application</title>
    <link rel="stylesheet" href="css/jquery.mobile-1.3.2.min.css">
    <link rel="stylesheet" href="css/index.css">
    <script src="js/jquery.js"></script>
    <script src="js/jquery.mobile-1.3.2.min.js"></script>
    <script src="js/mustache.js"></script>
    <script src="phonegap.js"></script>
</head>
<body onload="init()">
    <div data-role="page" id="main">
    <div data-role="header" class="logo">
        <img src="img/logo.png" />
    </div>
    <div data-role="content">
        <ul id="canlist" data-role="listview">

        </ul>
        <a href="#newcan" data-role="button" data-icon="plus">New can</a>

    </div>

    <div data-role="footer" data-theme="c">
        <h2 class="offline">Offline Mode</h2>
    </div>

</div>
<div data-role="page" id="newcan">
    <div data-role="header" class="logo">
        <img src="img/logo.png" />
    </div>
    <div id="candetailcontent" data-role="content">
    </div>
    <div data-role="footer" data-theme="c">
        <p>Snapcan!</p>
    </div>
</div>
</div>
<script type="text/javascript"  charset="utf-8">
        function init() {
            document.addEventListener("deviceready", onDeviceReady, false);
        }

        function onDeviceReady() {
            alert('ready');
            var networkstate = navigator.connection.type;
            if(networkstate == "none")
            {
                $(".offline").css("visibility","visible");
            }
        }
    </script>
</body>
</html>


我的index.css:

.offline{
    visibility: hidden;
    color:  #f00;
    font-style:italic;
}


如果没有连接,则应该可以看到红色的“脱机模式”,但是没有。

最佳答案

我认为您应该尝试使用cordova的离线事件监听器。

10-06 08:09