js判断网络连接情况:navigator.onLine

<body>
    <h1 id="text">websong</h1>
</body>
<script>
    var text=document.querySelector('#text').innerText;
    setInterval(function () {
        if (navigator.onLine) {
            //alert('online:有网络');
            document.querySelector('#text').style.color='red';
            document.querySelector('#text').innerText=text+'您的网络恢复了'
        } else {
            //alert('offline:断开网络');
            document.querySelector('#text').style.color='blue';
            document.querySelector('#text').innerText=text+'您断网了'
        }
    },1000)
</script>

  亲测在各个浏览器都可以跑,当然,低版本的我没试。

posted @ 2017-08-02 16:25  宋宇  阅读(743)  评论(0编辑  收藏  举报