JavaScript Navigator onLine Property

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline

What You’ll Learn

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.

01

Kind

Read-only property

02

Returns

Boolean

03

Baseline

Widely available

04

true

Looks online

05

false

Looks offline

06

Events

online / offline

Introduction

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.

⚠️
Treat it as a hint

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.

Understanding the onLine Property

Think 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.

  • true — the browser believes the device is online.
  • false — the browser believes the device is offline.
  • Read-only — part of the HTML Navigator interface.
  • Baseline — Widely available across modern browsers (MDN, since July 2015).
  • Events — listen on window for online and offline.

📝 Syntax

General form of the property:

JavaScript
navigator.onLine

Value

  • A Boolean: true (online) or false (offline).

Common patterns

JavaScript
// 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");
});

⚡ Quick Reference

GoalCode
Read the flagnavigator.onLine
Looks online?if (navigator.onLine) { … }
Looks offline?if (!navigator.onLine) { … }
Went offlinewindow.addEventListener("offline", …)
Came back onlinewindow.addEventListener("online", …)
Writable?No (read-only)
Status (MDN)Baseline Widely available

🔍 At a Glance

Four facts to remember about navigator.onLine.

Returns
boolean

true / false

Baseline
widely

Since July 2015

Access
read-only

No assignment

Best for
UX hints

Not hard locks

📋 onLine vs Events vs a Real Request

navigator.onLineonline / offline eventsfetch / XHR result
What it tells youBrowser’s connectivity guessWhen that guess changesWhether this request worked
SpeedInstant BooleanReactive (no poll)Needs a network round-trip
AccuracyHeuristic — can be wrongSame underlying heuristicGround truth for that URL
Best forInitial banner / status textLive UI updatesRetry, queue, or error handling
Use together?Yes — show hints from onLine + events, and always handle failed requests

Examples Gallery

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.

📚 Getting Started

Read the Boolean and apply the MDN online / offline check.

Example 1 — Read onLine

Log the current Boolean value.

JavaScript
console.log(navigator.onLine);
Try It Yourself

How It Works

Most everyday setups return true. Turn on airplane mode or DevTools Network → Offline to see false.

Example 2 — MDN if (navigator.onLine) Check

Branch with a clear online / offline message.

JavaScript
if (navigator.onLine) {
  console.log("online");
} else {
  console.log("offline");
}
Try It Yourself

How It Works

This matches MDN’s basic usage. Remember: “online” here means the browser’s guess, not a guaranteed Internet path.

📈 Practical Patterns

Live events, UI banners, and treating the flag as a soft hint.

Example 3 — Listen for online / offline

Update when the browser’s network status changes.

JavaScript
window.addEventListener("offline", () => {
  console.log("offline");
});

window.addEventListener("online", () => {
  console.log("online");
});

console.log("Listening… current: " + (navigator.onLine ? "online" : "offline"));
Try It Yourself

How It Works

MDN attaches listeners to window. In the try-it lab, toggle DevTools Offline to fire the events live.

Example 4 — Friendly Status Banner Text

Build a short string for a toast or top-of-page notice.

JavaScript
function networkBanner() {
  return navigator.onLine
    ? "You appear to be online."
    : "You appear to be offline — changes may sync later.";
}

console.log(networkBanner());
Try It Yourself

How It Works

Wording like “appear to be” matches MDN advice: inform the user without hard-blocking the UI.

Example 5 — Soft Hint Helper (Do Not Hard-Block)

Return a status object for UI — keep features available and handle real errors separately.

JavaScript
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));
Try It Yourself

How It Works

Keep allowUserActions: true so you never lock the app solely because of onLine. Pair with fetch error handling and optional offline queues.

🚀 Common Use Cases

  • Offline banners — tell users sync may wait until connectivity returns.
  • Draft / form apps — queue saves locally when the status looks offline.
  • PWA shell UI — update a status chip from online / offline events.
  • Diagnostics — include navigator.onLine in support debug panels.
  • Not for paywalls — never use this alone to enable or disable paid features.

🧠 How navigator.onLine Works

1

OS / browser heuristics

The engine estimates connectivity (LAN, adapters, OS checks).

Heuristic
2

Expose a Boolean

Your script reads navigator.onLine as true or false.

Boolean
3

Fire window events

When the guess changes, online or offline fires on window.

Events
4

Hint + real errors

Show soft UX tips, and still handle failed fetch / XHR.

📝 Notes

  • Baseline Widely available (MDN, since July 2015).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Read-only Boolean on Navigator.
  • LAN / virtual adapters may report online without Internet access.
  • Do not hard-disable features based only on onLine — provide hints and handle request failures.
  • Related: connection, cookieEnabled, Window, JavaScript hub.

Universal Browser Support

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.

Baseline · Widely available

Navigator.onLine

Read the Boolean for soft status UI. Listen for window online/offline events. Never treat the flag as proof that every request will succeed.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium & Legacy
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported in legacy IE (heuristic differs)
Full support
onLine Excellent

Bottom line: Check navigator.onLine, listen for online/offline, show soft warnings, and handle fetch failures — the flag is a hint, not a guarantee.

Conclusion

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.

💡 Best Practices

✅ Do

  • Use soft banners and status chips
  • Listen for online / offline on window
  • Queue work and retry when requests fail
  • Phrase copy as “appear offline” not absolute claims
  • Combine with fetch error handling

❌ Don’t

  • Hard-disable core features when false
  • Assume true means the Internet works
  • Try to assign to navigator.onLine
  • Ignore failed requests because the flag said online
  • Confuse this with navigator.connection bandwidth details

Key Takeaways

Knowledge Unlocked

Five things to remember about onLine

Baseline Boolean — browser network guess + events.

5
Core concepts
02

true

Looks online

OK
📡 03

Events

online / offline

Live
⚠️ 04

Accuracy

heuristic hint

Caution
🎯 05

Status

Baseline

Modern

❓ Frequently Asked Questions

It is a read-only Navigator property that returns a Boolean: true when the browser thinks the device is online, false when it thinks the device is offline.
No. MDN marks it as Baseline Widely available (since July 2015). It is a standard HTML Navigator property — not Deprecated, Experimental, or Non-standard.
Listen for the online and offline events on window with addEventListener. Those events fire when navigator.onLine changes.
No. Browsers and operating systems use different heuristics. A LAN or virtual adapter may count as online even without real Internet access. Treat the flag as a hint for UX, not as proof that a request will succeed.
MDN recommends against hard-disabling features based only on this flag. Prefer soft hints (banners, queued actions) and still handle network errors from fetch or XHR.
No. The property is read-only. You read the Boolean and listen for online/offline events; you do not assign to it.
Did you know?

On 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.

Learn oscpu Next

Non-standard OS identification string on Navigator.

oscpu →

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