Example 1 — Read onLine
Log the current Boolean value.
console.log(navigator.onLine); How It Works
Most everyday setups return true. Turn on airplane mode or DevTools Network → Offline to see false.

navigator.onLine is a read-only Boolean that reports whether the browser thinks the device is connected to a network. Learn the MDN check pattern, online / offline events, why the value is only a hint, five examples, and try-it labs.
Read-only property
Boolean
Widely available
Looks online
Looks offline
online / offline
Progressive web apps, editors, and forms often need a quick answer to “does the browser think we have a network?” navigator.onLine gives that Boolean in one read.
When the value changes, the browser fires an online or offline event on window. That is the usual way to update a status banner without polling.
MDN: the property is inherently unreliable. A LAN, VPN, firewall, or virtual adapter can make true mean “connected to something,” not “the public Internet works.” Use it for UX tips — not for hard feature locks.
onLine PropertyThink of onLine as the browser’s best guess about network connectivity. You read it; you never assign to it. Pair it with events and real request error handling.
Navigator interface.window for online and offline.General form of the property:
navigator.onLine true (online) or false (offline).// MDN basic check:
if (navigator.onLine) {
console.log("online");
} else {
console.log("offline");
}
// React to changes:
window.addEventListener("offline", () => {
console.log("offline");
});
window.addEventListener("online", () => {
console.log("online");
}); | Goal | Code |
|---|---|
| Read the flag | navigator.onLine |
| Looks online? | if (navigator.onLine) { … } |
| Looks offline? | if (!navigator.onLine) { … } |
| Went offline | window.addEventListener("offline", …) |
| Came back online | window.addEventListener("online", …) |
| Writable? | No (read-only) |
| Status (MDN) | Baseline Widely available |
Four facts to remember about navigator.onLine.
booleantrue / false
widelySince July 2015
read-onlyNo assignment
UX hintsNot hard locks
onLine vs Events vs a Real Requestnavigator.onLine | online / offline events | fetch / XHR result | |
|---|---|---|---|
| What it tells you | Browser’s connectivity guess | When that guess changes | Whether this request worked |
| Speed | Instant Boolean | Reactive (no poll) | Needs a network round-trip |
| Accuracy | Heuristic — can be wrong | Same underlying heuristic | Ground truth for that URL |
| Best for | Initial banner / status text | Live UI updates | Retry, queue, or error handling |
| Use together? | Yes — show hints from onLine + events, and always handle failed requests | ||
Examples follow MDN navigator.onLine patterns. Prefer Try It Yourself to inspect your browser. Use View Output for expected messaging. Toggle airplane mode or DevTools “Offline” to see changes.
Read the Boolean and apply the MDN online / offline check.
onLineLog the current Boolean value.
console.log(navigator.onLine); Most everyday setups return true. Turn on airplane mode or DevTools Network → Offline to see false.
if (navigator.onLine) CheckBranch with a clear online / offline message.
if (navigator.onLine) {
console.log("online");
} else {
console.log("offline");
} This matches MDN’s basic usage. Remember: “online” here means the browser’s guess, not a guaranteed Internet path.
Live events, UI banners, and treating the flag as a soft hint.
online / offlineUpdate when the browser’s network status changes.
window.addEventListener("offline", () => {
console.log("offline");
});
window.addEventListener("online", () => {
console.log("online");
});
console.log("Listening… current: " + (navigator.onLine ? "online" : "offline")); MDN attaches listeners to window. In the try-it lab, toggle DevTools Offline to fire the events live.
Build a short string for a toast or top-of-page notice.
function networkBanner() {
return navigator.onLine
? "You appear to be online."
: "You appear to be offline — changes may sync later.";
}
console.log(networkBanner()); Wording like “appear to be” matches MDN advice: inform the user without hard-blocking the UI.
Return a status object for UI — keep features available and handle real errors separately.
function networkHint() {
return {
looksOnline: navigator.onLine,
message: navigator.onLine
? "Network looks OK — still handle fetch errors."
: "Looks offline — queue actions and show a soft warning.",
// MDN: do not disable features based only on this flag
allowUserActions: true
};
}
console.log(JSON.stringify(networkHint(), null, 2)); Keep allowUserActions: true so you never lock the app solely because of onLine. Pair with fetch error handling and optional offline queues.
online / offline events.navigator.onLine in support debug panels.navigator.onLine WorksThe engine estimates connectivity (LAN, adapters, OS checks).
Your script reads navigator.onLine as true or false.
When the guess changes, online or offline fires on window.
Show soft UX tips, and still handle failed fetch / XHR.
Navigator.onLine — provide hints and handle request failures.navigator.onLine is Baseline Widely available across modern browsers (MDN: since July 2015). Use the Boolean and online/offline events for UX hints, and always handle real network errors.
Read the Boolean for soft status UI. Listen for window online/offline events. Never treat the flag as proof that every request will succeed.
Bottom line: Check navigator.onLine, listen for online/offline, show soft warnings, and handle fetch failures — the flag is a hint, not a guarantee.
navigator.onLine is a simple, Baseline Boolean for the browser’s online guess. Pair it with online / offline events for live UI, treat the value as a hint, and always handle real network errors.
Continue with oscpu, connection, or the JavaScript hub.
online / offline on windowfetch error handlingfalsetrue means the Internet worksnavigator.onLinenavigator.connection bandwidth detailsonLineBaseline Boolean — browser network guess + events.
boolean
TypeLooks online
OKonline / offline
Liveheuristic hint
CautionBaseline
ModernOn Windows, online status may depend on reaching a Microsoft connectivity check server. Firewalls or some VPNs can make a working Internet connection report as offline — another reason to treat onLine as a hint only.
Non-standard OS identification string on Navigator.
8 people found this page helpful