Chapter 22Offline Apps: Detecting When the User Is Connected

Perhaps the hardest part of building an app that works offline is actually determining when the user is offline.

In this chapter, we’ll cover the ononline and onoffline events of the window object and the window.navigator.onLine property. We’ll also talk about their limits as well as an alternative way to test for connectivity.

Determining Whether the User Is Online

The window.navigator.onLine property tells us whether or not the user is connected to a network. It returns a Boolean true or false value:

    console.log('We are connected!');
} else {
    console.log('We don't have connectivity.');

We can use this property with other offline APIs to determine when and whether ...

Get Jump Start HTML5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.