Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

ononline — handling the online event

The ononline attribute on body specifies JavaScript that handles the Window object's online event. It signals a change in the browser's assessment of network availability, but does not confirm that a particular server is reachable.

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

Syntax

<body ononline="JavaScript statements">

ononline is an event handler content attribute for online. When written on body, it sets a handler on Window, rather than handling an ordinary event targeted at body. Its value is JavaScript code; the available event parameter represents the event.

The browser fires online when navigator.onLine changes from false to true. This does not mean that a particular server is reachable or that a network request will succeed. Registering the handler alone does not invoke it for the initial connection state.

The corresponding JavaScript property is window.ononline. Assigning to it replaces that property's previous handler. An alternative is window.addEventListener('online', handler), which allows independent listener functions to be registered. Do not insert untrusted data into attribute code.

The example deliberately dispatches a synthetic event to window to check the attribute wiring without changing the connection. The expected paragraph text is online. This test does not change navigator.onLine or prove Internet access. A Content Security Policy can block handlers written directly in attributes.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>ononline demo</title>
</head>
<body ononline="document.getElementById('status').textContent = event.type">
  <p id="status">waiting</p>
  <script>
    window.dispatchEvent(new Event('online'));
  </script>
</body>
</html>

Result

online

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