HTML Navigator onLine 屬性
HTML 導航器 onLine 屬性返回一個布林值,該值定義瀏覽器是聯機還是離線。
語法
以下是語法 −
navigator.onLine
我們來看一個 HTML 導航器 onLine 屬性的示例 −
示例
<!DOCTYPE html> <html> <style> body { color: #000; height: 100vh; background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat; text-align: center; } .btn { background: #db133a; border: none; height: 2rem; border-radius: 20px; width: 330px; display: block; color: #fff; outline: none; cursor: pointer; margin: 1rem auto; } .show { font-size: 1.2rem; color: #fff; } </style> <body> <h1>HTML navigator onLine property Demo</h1> <button class="btn" onclick="display()">Check Browser Online/Offline Mode</button> <div class="show"></div> <script> function display() { var msg = document.querySelector(".show"); if (navigator.onLine) { msg.innerHTML = "Hey! You are online"; } else { msg.innerHTML = "Hey! You are offline"; } } </script> </body> </html>
輸出
單擊“檢查瀏覽器線上/離線模式”按鈕,以瞭解瀏覽器處於線上還是離線模式 −
現在嘗試斷開網路連線,然後再次單擊“檢查瀏覽器線上/離線模式”按鈕 −
廣告