The onoffline attribute is an event handler on <body> (and frameset) for the window offline event. It runs when the browser loses network access and Navigator.onLine switches to false.
Pair it with ononline when connectivity returns. Use the event to show a banner or queue work — not as the only way to know if the Internet works (navigator.onLine is a hint).
Baseline · Widely available.
Not deprecated and not experimental. Stable across modern browsers since about July 2015.
navigator.onLine is unreliable.
MDN: LAN, VPNs, and firewalls can misreport status. Treat offline/online as UX hints — do not hard-disable critical features based on onLine alone.
Syntax
HTML Syntax
syntax.html
<body onoffline="script">
Modern equivalent: window.addEventListener("offline", function () { … }); — listen on window, not with addEventListener on body alone.
Shows navigator.onLine right now. Use DevTools → Network → Offline for a real change, or simulate the offline / online events for practice (simulation does not flip onLine).
demo
Toggle DevTools Offline to fire real events, or use Simulate to dispatch them.
Connectivity
navigator.onLine: …
Last event: (none)
Code
HTML Examples
Short study snippets. Test with DevTools Offline, or use Try It Yourself.
Example 1 — Update Status on body
Show a message when the browser reports offline.
HTML
<body onoffline="setStatus('You are offline')">
<p id="status">Waiting…</p>
<p>Turn on DevTools → Network → Offline to test.</p>
<script>
function setStatus(text) {
document.getElementById("status").textContent = text;
}
</script>
</body>
Prefer this style in apps so multiple handlers can coexist.
Edge Cases & Pitfalls
Most bugs treat “browser says online” as “the Internet works.”
hint
onLine is a hint, not a guarantee
MDN: LAN without Internet, virtual adapters, firewalls, and VPNs can lie. Still handle failed fetches.
listen
addEventListener belongs on window
HTML attribute goes on body. Script listeners must use window — not body.addEventListener alone.
load
Check status on first paint
If the user opens the page already offline, offline may not fire again. Read navigator.onLine once at start.
features
Do not hard-disable the whole UI
Prefer soft warnings and queued actions. A false “offline” report should not brick the app.
pair
Always pair with online
Clear banners and retry syncs in ononline when connectivity returns.
Compatibility
Browser Support
onoffline is an HTML event handler for the window offline event. It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
onoffline
Widely available since about July 2015. Supported across modern browsers; remember Navigator.onLine heuristics still vary by OS.
98%Global support (approx.)
Google ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetModern
Full support
Internet ExplorerLegacy support
Supported
onofflineWidely supported
Bottom line: Safe to use for offline UX hints. Prefer addEventListener on window in apps; always pair with online and real request error handling.
Remember
Key Takeaways
Role: run script when the browser reports lost network access.
HTML: put onoffline on body; scripts listen on window.
Pair: use ononline when connectivity returns.
Caution:navigator.onLine is a hint — still handle failed requests.
One line: use onoffline to warn and queue — not as the only truth about the Internet.
Frequently Asked Questions
It is an event handler (usually on body) for the window offline event. The browser runs your script when it loses network access and Navigator.onLine switches to false.
onoffline runs when the connection is lost. ononline runs when connectivity returns and Navigator.onLine becomes true.
Put the HTML attribute on body (or frameset). For addEventListener, listen on window. Listening only on body with addEventListener will not receive the event.
Not fully. MDN warns browsers use heuristics (LAN can look “online” without Internet). Use offline/online as hints — do not hard-disable critical features based on onLine alone.
In Chrome/Edge DevTools: Network panel → Offline (or throttling). Firefox has a similar Network Offline option. You can also dispatch a synthetic offline Event for demos.
No. The offline event is Baseline widely available. Prefer window.addEventListener("offline", …) in modern scripts; the body attribute is the HTML form of the same handler.