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.