Online and offline events

1852 단어 htmlOperafirefox

<!doctype html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Online and offline events</title>
 </head>
 <body>
   <div id="status"><p id="state"></p></div>
   <div id="log"></div>
   <div>ps:Firefox 3 introduces two new events: <a href="https://developer.mozilla.org/en/Online_and_offline_events">"online" and "offline"</a>. </div>
<script>
 function updateOnlineStatus(msg) {
   var status = document.getElementById("status");
   var condition = navigator.onLine ? "ONLINE" : "OFFLINE";
   status.setAttribute("class", condition);
   var state = document.getElementById("state");
   state.innerHTML = condition;
   var log = document.getElementById("log");
   log.innerHTML+="Event: " + msg + "; status=" + condition + "<br/>";
 }
 function loaded() {
   updateOnlineStatus("load");
   if(!window.addEventListener){//         ,  ie8,(ie6,7   )
    document.addEventListener=function(etype,callback){
        document.bdoy.attachEvent("on"+etype,callback);//IE8   document.body,   document window
    }
   }
   document.addEventListener("offline", function () {//opera 10.53   ,chrome5,safari4     
     updateOnlineStatus("offline")
   }, false);
   document.addEventListener("online", function () {
     updateOnlineStatus("online")
   }, false);
 }
 window.onload=loaded;
</script>
 </body>
 </html>

좋은 웹페이지 즐겨찾기