Web APIs (navigator, geolocation, history)
How can a web application detect whether it has network connectivity?
Web applications can detect network connectivity using navigator.onLine: 1) This property returns a boolean indicating whether the browser has network access, 2) true indicates the browser is online, false indicates offline mode, 3) The property is updated automatically by the browser when connectivity changes, 4) Changes can be detected by listening for the 'online' and 'offline' events on the window object, 5) This is more reliable than actively pinging servers, which can fail for reasons other than connectivity, 6) The exact behavior varies somewhat between browsers—some consider a captive portal as 'offline' until authenticated, 7) It's important to note that true doesn't guarantee internet connectivity, only that the network interface is up, 8) This API is widely supported across browsers and provides a simple way to implement offline-first functionality.