Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

onoffline — handling the offline transition event

The onoffline attribute on body sets a handler for the Window object's offline event. It lets a page react when the browser reports lost connectivity, but does not check whether a particular server is reachable.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<body onoffline="JavaScript statements">

onoffline is an event handler attribute: its value is JavaScript code, not a boolean. When placed on body, it handles the Window object's offline event, not an event concerning only the body element.

The browser queues an offline event when navigator.onLine changes from true to false. Setting the attribute does not check the initial state. Read navigator.onLine during initialization; use ononline to react to the opposite transition.

This signal is not a server-reachability test. A value of true does not guarantee Internet access, and request failures must be handled independently. Do not disable essential functionality solely on the basis of this indicator.

An alternative to the attribute is window.addEventListener('offline', handler), which separates JavaScript from HTML and permits multiple listeners. A Content Security Policy can block handler code written directly in an attribute.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>onoffline demonstration</title>
</head>
<body onoffline="document.getElementById('status').textContent = 'offline event received'">
  <p id="status" role="status">waiting</p>
  <button id="simulate" type="button">Simulate offline event</button>
  <script>
    document.getElementById('simulate').addEventListener('click', () => {
      window.dispatchEvent(new Event('offline'));
    });
  </script>
</body>
</html>

Result

offline event received

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources