Events
onoffline
The browser reports that its network connectivity state is offline. Window event handler, exposed through the body element in HTML; register on window rather than on an arbitrary element. Event interface: Event. For the described native event: bubbles: no; cancelable: no. navigator.onLine is only a hint; actual request success must be checked separately. Example: Change browser network state; delivery varies. The event listener is registered with addEventListener(), which avoids inline script handlers and supports multiple listeners. Inline handlers require a CSP that permits them; do not insert untrusted code into handler attributes.
Formal Syntax & Structure
html syntaxhtml
<body onoffline="handleEvent(event)"> window.addEventListener("offline", event => { /* handle event */ });Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| onoffline | JavaScript handler body | Optional HTML event-handler code; the example uses a registered listener instead. Not a Boolean attribute. |
| event | Event | Event object delivered to the addEventListener callback; event.type is "offline". Inspect target, currentTarget, bubbles and cancelable as appropriate. |
Working Example
Example Snippethtml
<p>Window event monitor</p><p role="status" id="log">Waiting for offline</p><script> const target = window; const log = document.getElementById("log"); target.addEventListener("offline", event => { log.textContent = String("Online hint: " + navigator.onLine); });</script>