HTML ononline Attribute

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

Definition and Usage

The ononline attribute is an event handler on <body> (and frameset) for the window online event. It runs when the browser gains network access and Navigator.onLine switches to true.

Pair it with onoffline when the connection drops. Use online to clear banners, retry syncs, or refresh data — not to prove a specific website is reachable.

Baseline · Widely available.

Not deprecated and not experimental. Stable across modern browsers since about July 2015.

Online ≠ site reachable.

MDN: network problems or firewalls can still block your site even when the browser reports online. Still handle failed requests.

HTML Syntax

syntax.html
<body ononline=" script">

Modern equivalent: window.addEventListener("online", 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 onlineTry it
With onofflineHide offline banner; retry work when back onlineTry it
addEventListener("online")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, then turn it off to fire a real online event — or simulate events for practice (simulation does not flip onLine).

demo

Leave Offline mode in DevTools to fire a real online event, or use Simulate.

Connectivity

navigator.onLine: …

Last event: (none)

HTML Examples

Short study snippets. Test with DevTools Offline (then reconnect), or use Try It Yourself.

Example 1 — Update Status on body

Show a message when the browser reports online again.

HTML
<body ononline="setStatus('You are online')">
  <p id="status">Waiting…</p>
  <p>Go Offline in DevTools, then turn Offline off to test.</p>

  <script>
    function setStatus(text) {
      document.getElementById("status").textContent = text;
    }
  </script>
</body>
Try It Yourself

How It Works

  1. Put ononline on body — that registers the window handler.
  2. Browser gains connection — Navigator.onLine becomes true.
  3. online fires — your script updates the status text.
  4. Test tip: enable DevTools Offline, then disable it to fire online.

Example 2 — Clear Banner and Retry Sync

Hide an offline notice and pretend to retry work when back online.

HTML
<body
  onoffline="showBanner(true)"
  ononline="onBackOnline()">
  <p id="banner" hidden>Offline — changes may not sync.</p>
  <p id="log">Ready.</p>

  <script>
    function showBanner(offline) {
      document.getElementById("banner").hidden = !offline;
    }
    function onBackOnline() {
      showBanner(false);
      document.getElementById("log").textContent =
        "Online again — retry sync / refetch here.";
    }
    if (!navigator.onLine) showBanner(true);
  </script>
</body>
Try It Yourself

How It Works

  1. onoffline shows the banner while disconnected.
  2. ononline hides the banner and runs “retry” logic.
  3. Check navigator.onLine on load — the user may already be offline.
  4. Still handle fetch errors — “online” does not guarantee your API works.

Example 3 — window.addEventListener("online")

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("online", function () {
    refresh();
    add("online event");
  });
  window.addEventListener("offline", function () {
    refresh();
    add("offline event");
  });
</script>
Try It Yourself

How It Works

  1. Listen on window for online and offline.
  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 “my API is reachable.”

reach

Online does not mean your site works

MDN: firewalls or partial outages can still block requests. Retry carefully and surface real errors.

hint

onLine is a heuristic

LAN without Internet, VPNs, and OS checks can misreport. Use events as UX signals, not hard gates.

listen

addEventListener belongs on window

HTML attribute goes on body. Script listeners must use window — not body.addEventListener alone.

load

Already online at start

online fires on a change to online. If the page loads already online, check navigator.onLine once yourself.

pair

Always pair with offline

Show warnings in onoffline; clear them and retry in ononline.

Browser Support

ononline is an HTML event handler for the window online event. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

ononline

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
ononline Widely supported

Bottom line: Safe to use for “back online” UX. Prefer addEventListener on window in apps; always verify important requests succeed.

Key Takeaways

  • Role: run script when the browser reports network access restored.
  • HTML: put ononline on body; scripts listen on window.
  • Pair: use onoffline when connectivity drops.
  • Caution: online ≠ your website is reachable — still handle errors.

One line: use ononline to resume and retry — then confirm the request actually succeeds.

Frequently Asked Questions

It is an event handler (usually on body) for the window online event. The browser runs your script when it gains network access and Navigator.onLine switches to true.
ononline runs when connectivity returns. onoffline runs when the connection is lost and Navigator.onLine becomes false.
No. MDN: do not use this event to decide if a particular site is available. Firewalls or network problems can still block requests even when the browser reports online.
Put the HTML attribute on body (or frameset). For addEventListener, listen on window. Listening only on body with addEventListener will not receive the event.
Enable DevTools → Network → Offline, then turn Offline off again. That fires offline then online. You can also dispatch a synthetic online Event for demos.
No. The online event is Baseline widely available. Prefer window.addEventListener("online", …) in modern scripts; the body attribute is the HTML form of the same handler.

Next: onpagehide

Handle the moment a page is hidden or leaving the session.

onpagehide 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