Revision 178469 of Eventos online e offline

  • Slug da revisão: Eventos_online_e_offline
  • Título da revisão: Eventos online e offline
  • ID da revisão: 178469
  • Criado:
  • Autor: Verruckt
  • É revisão atual? Não
  • Comentário New page: {{fx minversion header|3}} <breadcrumbs></breadcrumbs> O [[Firefox 3 for developers|Firefox 3]] implementou [http://www.whatwg.org/specs/web-apps/current-work/#offline eventos Online/Offli...

Conteúdo da revisão

{{template.Fx_minversion_header(3)}} O Firefox 3 implementou eventos Online/Offline da especificação WHATWG Web Applications 1.0.

Visão geral

In order to build a good offline-capable web application, you need to know when your application is actually offline. Incidentally, you also need to know when your application has returned to an 'online' status again. Effectively, the requirements break down as such:

  1. You need to know when the user comes back online, so that you can re-synchronize with the server.
  2. You need to know when the user is offline, so that you can be sure to queue your server requests for a later time.

It is this process that online/offline events help to trivialize.

API

navigator.onLine

navigator.onLine is a property that maintains a true/false value (true for online, false for offline). This property is updated whenever the user switches into "Offline Mode" by selecting the corresponding menu item (File -> Work Offline in Firefox).

Additionally, this property should update whenever a browser is no longer capable of connecting to the network. According to the specification:

The navigator.onLine attribute must return false if the user agent will not contact the network when the user follows links or when a script requests a remote page (or knows that such an attempt would fail)...

Firefox 2 updates this property when switching to/from the browser's Offline mode, and when losing/regaining network connectivity in Windows and Linux.

This property existed in older versions of Firefox and Internet Explorer (the specification based itself off of these prior implementations), so you can begin using it immediately. Network status autodetection was implemented in Firefox 2.

"online" and "offline" events

Firefox 3 introduces two new events: "online" and "offline". These two events are fired on the <body> of each page when the browser switches between online and offline mode. Additionally, the events bubble up from document.body, to document, ending at window. Both events are non-cancellable (you can't prevent the user from coming online, or going offline).

You can register listeners for these events in a few familiar ways:

  • using addEventListener on the window, document, or document.body
  • by setting the .ononline or .onoffline properties on document or document.body to a JavaScript Function object. (Note: using window.ononline or window.onoffline will not work for compatibility reasons.)
  • by specifying ononline="..." or onoffline="..." attributes on the <body> tag in the HTML markup.

Exemplo

There's a simple test case that you can run to verify that the events are working. XXX When mochitests for this are created, point to those instead and update this example -nickolay

 <!doctype html>
 <html>
 <head>
   <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.appendChild(document.createTextNode("Event: " + msg + "; status=" + condition + "\n"));
     }
     function loaded() {
       updateOnlineStatus("load");
       document.body.addEventListener("offline", function () {
         updateOnlineStatus("offline")
       }, false);
       document.body.addEventListener("online", function () {
         updateOnlineStatus("online")
       }, false);
     }
   </script>
   <style>...</style>
 </head>
 <body onload="loaded()">
   <div id="status"><p id="state"></p></div>
   <div id="log"></div>
 </body>
 </html>

Referências

Categorias

Interwiki Language Links

{{ wiki.languages( { "en": "en/Online_and_offline_events", "fr": "fr/\u00c9v\u00e8nements_online_et_offline" } ) }}

Fonte da revisão

<p>{{template.Fx_minversion_header(3)}}

O <a href="pt/Firefox_3_for_developers">Firefox 3</a> implementou <a class="external" href="http://www.whatwg.org/specs/web-apps/current-work/#offline">eventos Online/Offline</a> da <a class="external" href="http://www.whatwg.org/specs/web-apps/current-work/">especificação WHATWG Web Applications 1.0</a>.
</p>
<h3 name="Vis.C3.A3o_geral"> Visão geral </h3>
<p>In order to build a good offline-capable web application, you need to know when your application is actually offline. Incidentally, you also need to know when your application has returned to an 'online' status again. Effectively, the requirements break down as such:
</p>
<ol><li> You need to know when the user comes back online, so that you can re-synchronize with the server.
</li><li> You need to know when the user is offline, so that you can be sure to queue your server requests for a later time.
</li></ol>
<p>It is this process that online/offline events help to trivialize.
</p>
<h3 name="API"> API </h3>
<h4 name="navigator.onLine"> <code>navigator.onLine</code> </h4>
<p><code><a href="pt/DOM/window.navigator.onLine">navigator.onLine</a></code> is a property that maintains a <code>true</code>/<code>false</code> value (<code>true</code> for online, <code>false</code> for offline). This property is updated whenever the user switches into "Offline Mode" by selecting the corresponding menu item (File -&gt; Work Offline in Firefox).
</p><p>Additionally, this property should update whenever a browser is no longer capable of connecting to the network. According to the specification:
</p>
<blockquote cite="http://www.whatwg.org/specs/web-apps/current-work/#offline">
The <code>navigator.onLine</code> attribute must return false if the user agent will not contact the network when the user follows links or when a script requests a remote page (or knows that such an attempt would fail)...
</blockquote>
<p>Firefox 2 updates this property when switching to/from the browser's Offline mode, and when losing/regaining network connectivity in Windows and Linux.
</p><p>This property existed in older versions of Firefox and Internet Explorer (the specification based itself off of these prior implementations), so you can begin using it immediately. Network status autodetection was implemented in Firefox 2.
</p>
<h4 name=".22online.22_and_.22offline.22_events"> "<code>online</code>" and "<code>offline</code>" events </h4>
<p><a href="pt/Firefox_3">Firefox 3</a> introduces two new events: "<code>online</code>" and "<code>offline</code>". These two events are fired on the <code>&lt;body&gt;</code> of each page when the browser switches between online and offline mode. Additionally, the events bubble up from <code>document.body</code>, to <code>document</code>, ending at <code>window</code>. Both events are non-cancellable (you can't prevent the user from coming online, or going offline).
</p><p>You can register listeners for these events in a few familiar ways:
</p>
<ul><li> using <code><a href="pt/DOM/element.addEventListener">addEventListener</a></code> on the <code>window</code>, <code>document</code>, or <code>document.body</code>
</li><li> by setting the <code>.ononline</code> or <code>.onoffline</code> properties on <code>document</code> or <code>document.body</code> to a JavaScript <code>Function</code> object. (<b>Note:</b> using <code>window.ononline</code> or <code>window.onoffline</code> will not work for compatibility reasons.)
</li><li> by specifying <code>ononline="..."</code> or <code>onoffline="..."</code> attributes on the <code>&lt;body&gt;</code> tag in the HTML markup.
</li></ul>
<h3 name="Exemplo"> Exemplo </h3>
<p>There's <a class="external" href="https://bugzilla.mozilla.org/attachment.cgi?id=220609">a simple test case</a> that you can run to verify that the events are working.
<span class="comment">XXX When mochitests for this are created, point to those instead and update this example -nickolay</span>
</p>
<pre class="eval"> &lt;!doctype html&gt;
 &lt;html&gt;
 &lt;head&gt;
   &lt;script&gt;
     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.appendChild(document.createTextNode("Event: " + msg + "; status=" + condition + "\n"));
     }
     function loaded() {
       updateOnlineStatus("load");
       document.body.addEventListener("offline", function () {
         updateOnlineStatus("offline")
       }, false);
       document.body.addEventListener("online", function () {
         updateOnlineStatus("online")
       }, false);
     }
   &lt;/script&gt;
   &lt;style&gt;...&lt;/style&gt;
 &lt;/head&gt;
 &lt;body onload="loaded()"&gt;
   &lt;div id="status"&gt;&lt;p id="state"&gt;&lt;/p&gt;&lt;/div&gt;
   &lt;div id="log"&gt;&lt;/div&gt;
 &lt;/body&gt;
 &lt;/html&gt;
</pre>
<h3 name="Refer.C3.AAncias"> Referências </h3>
<ul><li> <a class="external" href="http://www.whatwg.org/specs/web-apps/current-work/#offline">'Online/Offline events' section from the WHATWG Web Applications 1.0 Specification</a>
</li><li> <a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=336359">The bug tracking online/offline events implementation in Firefox</a> and a <a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=336682">follow-up</a>
</li><li> <a class="external" href="https://bugzilla.mozilla.org/attachment.cgi?id=220609">A simple test case</a>
</li><li> <a class="external" href="http://ejohn.org/blog/offline-events/">An explanation of Online/Offline events</a>
</li></ul>
<p><span class="comment">Categorias</span>
</p><p><span class="comment">Interwiki Language Links</span>
</p>{{ wiki.languages( { "en": "en/Online_and_offline_events", "fr": "fr/\u00c9v\u00e8nements_online_et_offline" } ) }}
Reverter para esta revisão