<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <title>html5 js 监听网络在线与离线</title> </head> <body> <div id="status" style="font-size:100px;"></div> </body> </html> <script type="text/javascript" language="javascript" charset="utf-8"> $$ = function(id){return document.getElementById(id);}; if(navigator.onLine){ $$("status").innerHTML="第一次加载时在线"; } else{ $$("status").innerHTML="第一次加载时离线"; } window.addEventListener("online", online, false); function online(){ $$("status").innerHTML="on"; } window.addEventListener("offline", offline, false); function offline(){ $$("status").innerHTML="off"; } </script>
时间: 2024-11-11 10:49:32