Categories
JavaScript Answers

How to detect online or offline event cross-browser with JavaScript?

Spread the love

Sometimes, we want to detect online or offline event cross-browser with JavaScript.

In this article, we’ll look at how to detect online or offline event cross-browser with JavaScript.

How to detect online or offline event cross-browser with JavaScript?

To detect online or offline event cross-browser with JavaScript, we can use the navigator.onLine property.

For instance, we write

if (navigator.onLine) {
  // online
} else {
  // offline
}

to check the navigator.onLine value with the if statement.

If navigator.onLine is true, then the device is online.

Otherwise, it’s offline.

Conclusion

To detect online or offline event cross-browser with JavaScript, we can use the navigator.onLine property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *