Course contentsShow
← Web Development
Lesson 1697 of 2,98231. Error Handling and ResilienceFree lesson

The navigator.onLine Property

Understanding the navigator.onLine boolean and its limitations in detecting true network connectivity.

The navigator.onLine Property

You'll learn: How to use navigator.onLine to check network connectivity status and understand its important limitations.

What Is navigator.onLine?

navigator.onLine is a built-in browser property that returns a boolean value indicating whether the browser believes it has a network connection. It returns true when online and false when offline.

Think of it like a light on your router: it tells you if the cable is plugged in, but not whether your internet service provider is actually working or if you can reach specific websites.

How to Use It

if (navigator.onLine) {
  console.log("Browser thinks we're online");
} else {
  console.log("Browser is definitely offline");
}

You can check this property at any time to make decisions about whether to attempt network requests, show offline messages, or enable/disable features.

Critical Limitations

Here's the catch: navigator.onLine only tells you if the device has a network adapter connection. It does NOT guarantee:

  • Actual internet connectivity
  • Ability to reach your server
  • Working DNS resolution
  • Non-captive portal networks (like coffee shop WiFi login pages)

A true value means "probably online," but a failed request is still possible. A false value is more reliable—you're definitely offline.

Practical Application

Use navigator.onLine as a first-line check before attempting requests, but always handle network failures gracefully with the retry logic and error handling you've already learned.

if (!navigator.onLine) {
  showOfflineMessage();
} else {
  // Still need proper error handling!
  fetchData().catch(handleNetworkError);
}

Key Takeaway: navigator.onLine provides a basic connectivity hint but cannot guarantee actual internet access—always implement robust error handling regardless of its value.