HTML onoffline Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

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.

HTML Syntax

syntax.html
<body onoffline=" script">

Modern equivalent: window.addEventListener("offline", function () { … }); — listen on window, not with addEventListener on body alone.

Attribute Values

The attribute value is a script. Common patterns:

Value / patternDescriptionTry it
handler() on bodyRun code when the browser reports offlineTry it
With ononlineShow offline banner; clear it when back onlineTry it
addEventListener("offline")Preferred modern registration on windowTry it
navigator.onLineCurrent boolean hint (check on load + on events)Try it

Live Preview

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)

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>
Try It Yourself

How It Works

  1. Put onoffline on body — that registers the window handler.
  2. Browser loses connection — Navigator.onLine becomes false.
  3. offline fires — your script updates the status text.
  4. Test tip: DevTools Network → Offline is the easiest way to trigger it.

Example 2 — Offline Banner with ononline

Show a notice when offline; hide it when back online.

HTML
<body
  onoffline="showBanner(true)"
  ononline="showBanner(false)">
  <p id="banner" hidden>Offline — changes may not sync.</p>
  <p>Main page content…</p>

  <script>
    function showBanner(offline) {
      document.getElementById("banner").hidden = !offline;
    }
    if (!navigator.onLine) showBanner(true);
  </script>
</body>
Try It Yourself

How It Works

  1. onoffline shows the banner when the connection drops.
  2. ononline hides it when connectivity returns.
  3. Check navigator.onLine on load — the user may already be offline.
  4. Keep the banner as a hint — still handle failed network requests in your app logic.

Example 3 — window.addEventListener("offline")

Modern registration on window, with a live navigator.onLine readout.

HTML
<p id="line">Checking…</p>
<ul id="log"></ul>

<script>
  function refresh() {
    document.getElementById("line").textContent =
      "navigator.onLine = " + navigator.onLine;
  }

  function add(msg) {
    var li = document.createElement("li");
    li.textContent = msg;
    document.getElementById("log").appendChild(li);
  }

  refresh();
  window.addEventListener("offline", function () {
    refresh();
    add("offline event");
  });
  window.addEventListener("online", function () {
    refresh();
    add("online event");
  });
</script>
Try It Yourself

How It Works

  1. Listen on window for offline and online.
  2. Refresh navigator.onLine whenever an event fires.
  3. Log history so you can see the order of changes.
  4. 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.

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 Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer Legacy support
Supported
onoffline Widely 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.

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.

Next: ononline

Handle the moment network access comes back.

ononline tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful