👀 Live Preview
Current connection state (go offline in DevTools, then back online to test):
Checking connection…

The ononline attribute is an inline event handler for the online event on the window. It runs when the browser detects that network connectivity was restored after being offline. Use it to hide offline banners, re-enable sync, retry failed requests, or refresh stale data. Pair it with onoffline when connectivity drops. Check navigator.onLine for the current state. Put ononline on <body> or use window.addEventListener("online", …) in script.
Inline JS.
Back online.
Global scope.
Current state.
Full UX pair.
Flush queue.
ononline AttributeThe primary purpose of ononline is to react when the device or browser regains network access — so your app can hide offline messaging, resume background sync, retry API calls that failed while offline, and restore a normal user experience.
The online event fires on the window object. HTML allows window event handlers on <body> for legacy convenience. Like onoffline, this is a browser hint — verify server reachability with a real request when it matters.
Show offline UI in onoffline and tear it down in ononline. Users should always see consistent connection status.
Set ononline on body or assign window.ononline in JavaScript:
<body onoffline="handleOffline()" ononline="handleOnline()">
…
</body>
<script>
window.ononline = handleOnline;
window.onoffline = handleOffline;
</script>online event fires on the window.<body> as a window event content attribute.window.ononline in script.onoffline to show offline UI when connectivity drops.navigator.onLine on page load for initial state.window.addEventListener("online", handler).The ononline attribute accepts a string of JavaScript code:
ononline="handleOnline()" — Call a named function.ononline="document.getElementById('banner').hidden = true" — Hide offline banner inline.window.ononline = () => { … } — property assignment.function handleOnline() {
banner.hidden = true;
status.textContent = "Back online — syncing…";
retryPendingRequests();
}
window.addEventListener("online", handleOnline);
window.addEventListener("offline", handleOffline);| Property / event | When it fires | Notes |
|---|---|---|
online | Connectivity restored | ononline |
offline | Connectivity lost | onoffline |
navigator.onLine | Any time (read property) | true / false |
| Event target | window | Global browser event |
| Handler attribute | ononline on body | Window event handler |
| Target | Supported? | Notes |
|---|---|---|
<body> | Yes | Window event on body (HTML) |
window | Yes | Primary target in JavaScript |
<input>, <div> | No | Not an input event — use window |
<frameset> | Yes (legacy) | Rare today |
| Service Worker / PWA | Related | Background sync when online |
ononline vs onoffline vs navigator.onLine| API | Type | Typical use |
|---|---|---|
ononline | Event handler | Hide banner, retry sync |
onoffline | Event handler | Show offline banner |
navigator.onLine | Boolean property | Check state on load |
Successful fetch | Network response | Confirm server is reachable |
Inline body handler, window.ononline assignment, and reconnect sync with navigator.onLine.
Current connection state (go offline in DevTools, then back online to test):
Checking connection…
Update status when the browser reconnects:
<body onoffline="handleOffline()" ononline="handleOnline()">
<p id="status">Online</p>
</body>
<script>
function handleOnline() {
document.getElementById("status").textContent =
"Back online!";
}
function handleOffline() {
document.getElementById("status").textContent =
"Offline";
}
</script>The browser fires online on the window when connectivity is restored. The body attribute runs handleOnline() at that moment.
Assign window.ononline (same pattern as the reference tutorial):
<script>
function handleOnline() {
document.getElementById("log").textContent =
"Your browser is back online!";
}
window.ononline = handleOnline;
</script>window.ononline = handleOnline registers the same handler as an inline ononline attribute on body.
When online returns, hide the offline banner and flush a pending queue:
function handleOnline() {
banner.hidden = true;
status.textContent = "Syncing pending changes…";
flushQueue();
}
function handleOffline() {
banner.hidden = false;
status.textContent = "Offline — changes queued.";
}
window.addEventListener("online", handleOnline);
window.addEventListener("offline", handleOffline);ononline is the right moment to retry API calls and background sync that were paused while offline.
aria-live="polite" on status regions when online returns.Banner shown.
Wi‑Fi / cell on.
ononline runs.
Hide banner, sync.
The online event and ononline handler are supported in all modern browsers, including Internet Explorer 9+.
ononline supportAll major browsers fire online on the window when connectivity returns.
Bottom line: Reliable for reconnect UX in all modern browsers.
ononline with onofflineaddEventListener("online", …) in productionalert() when the user comes back onlineonline means your API is healthyononline on input elements — use windowThe ononline attribute runs JavaScript when the browser regains network connectivity — essential for hiding offline UI, retrying sync, and restoring a smooth user experience.
Pair it with onoffline, check navigator.onLine on load, and prefer addEventListener in production code.
ononlineBookmark these before building reconnect logic.
Global.
ScopePair both.
PatternSnapshot.
APIFlush queue.
UXAlso fetch.
Gotchaonline event fires — when the browser detects that network connectivity was restored.body or window.addEventListener("online", …).online event fires when connectivity returns.window.addEventListener("online", handler) is preferred for production code.Practice the ononline attribute with connection status examples in the Try It editor.
5 people found this page helpful