👀 Live Preview
Current connection state (toggle airplane mode or disable network to test offline):
Checking connection…

The onoffline attribute is an inline event handler for the offline event on the window. It runs when the browser detects that network connectivity was lost. Use it to show an offline banner, pause sync, or queue actions until the user is back online. Pair it with ononline when connectivity returns. Check navigator.onLine for the current state. Put onoffline on <body> (HTML maps body window-event attributes to the window) or use window.addEventListener("offline", …) in script.
Inline JS.
No network.
Global scope.
Current state.
Restore UX.
Preferred way.
onoffline AttributeThe primary purpose of onoffline is to react when the device or browser loses network access — so your app can warn the user, disable submit buttons, switch to cached data, or show a “You are offline” message instead of failing silently.
The offline event fires on the window object. HTML allows window event handlers on <body> for legacy convenience. The event is a hint — always combine with navigator.onLine and robust fetch error handling for production apps.
navigator.onLine can be true while the server is unreachable. Use failed fetch requests as a second signal in real apps.
Set onoffline on body or assign window.onoffline in JavaScript:
<body onoffline="handleOffline()" ononline="handleOnline()">
…
</body>
<script>
window.onoffline = handleOffline;
window.ononline = handleOnline;
</script>offline event fires on the window.<body> as a window event content attribute.window.onoffline in script.ononline to hide offline UI when back online.navigator.onLine on page load for initial state.window.addEventListener("offline", handler).The onoffline attribute accepts a string of JavaScript code:
onoffline="showOfflineBanner()" — Call a named function.onoffline="document.getElementById('banner').hidden = false" — Inline statement.window.onoffline = () => { … } — property assignment.function updateConnectionStatus() {
const online = navigator.onLine;
banner.hidden = online;
status.textContent = online ? "Online" : "Offline";
}
window.addEventListener("offline", updateConnectionStatus);
window.addEventListener("online", updateConnectionStatus);
updateConnectionStatus();| Property / event | When it fires | Notes |
|---|---|---|
offline | Connectivity lost | onoffline |
online | Connectivity restored | ononline |
navigator.onLine | Any time (read property) | true / false |
| Event target | window | Global browser event |
| Handler attribute | onoffline on body | Window event handler |
| Target | Supported? | Notes |
|---|---|---|
<body> | Yes | Window event on body (HTML) |
window | Yes | Primary target in JavaScript |
<div>, <button> | No | Use window listeners |
<frameset> | Yes (legacy) | Rare today |
| Service Worker / PWA | Related | Cache + offline pages |
onoffline vs ononline vs navigator.onLine| API | Type | Typical use |
|---|---|---|
onoffline | Event handler | Show offline banner |
ononline | Event handler | Hide banner, retry sync |
navigator.onLine | Boolean property | Check state on load |
Failed fetch | Network error | Real server reachability |
Inline body handler, addEventListener on window, and offline banner with navigator.onLine.
Current connection state (toggle airplane mode or disable network to test offline):
Checking connection…
Show feedback when the browser goes offline:
<body onoffline="handleOffline()" ononline="handleOnline()">
<p id="status">Online</p>
</body>
<script>
function handleOffline() {
document.getElementById("status").textContent = "Offline";
}
function handleOnline() {
document.getElementById("status").textContent = "Online";
}
</script>The browser fires offline on the window. The body attribute runs your handler when that happens.
Assign window.onoffline or use addEventListener:
<script>
window.onoffline = function () {
document.getElementById("log").textContent =
"Your browser is now offline.";
};
window.ononline = function () {
document.getElementById("log").textContent =
"Back online.";
};
</script>Property assignment on window is equivalent to the inline body handler for the offline event.
Show a banner when offline and hide it when online:
function syncBanner() {
banner.hidden = navigator.onLine;
banner.textContent = navigator.onLine
? "" : "You are offline — changes will sync later.";
}
window.addEventListener("offline", syncBanner);
window.addEventListener("online", syncBanner);
syncBanner();Call syncBanner() on load and on both offline and online events so the UI always matches navigator.onLine.
aria-live="polite" on status regions when connectivity changes.App works normally.
Wi‑Fi / cell off.
onoffline runs.
Banner, queue.
The offline event and onoffline handler are supported in all modern browsers, including Internet Explorer 9+.
onoffline supportAll major browsers fire offline on the window when connectivity changes.
Bottom line: Reliable for offline detection UX in all modern browsers.
onoffline with ononlinenavigator.onLine on page loadaddEventListener("offline", …) in productionalert() when the user goes offlinenavigator.onLine alone for server healthonoffline on random divs — use windowonline firesThe onoffline attribute runs JavaScript when the browser loses network connectivity — essential for offline banners, queued actions, and resilient web apps.
Pair it with ononline, check navigator.onLine on load, and prefer addEventListener in production code.
onofflineBookmark these before building offline-aware apps.
Global.
ScopePair both.
PatternSnapshot.
APIInform user.
UXAlso fetch.
Gotchaoffline event fires — when the browser detects that network connectivity was lost.body (window event attribute) or window.addEventListener("offline", …) in JavaScript.true if the browser thinks it is online, false if offline. Read it anytime; events tell you when it changes.window.addEventListener("offline", handler) is preferred for production code.Practice the onoffline attribute with connection status examples in the Try It editor.
5 people found this page helpful