HTML and CSS Reference
In-Depth Information
if (navigator.onLine) {
alert("You are Online");
}
else {
alert("You are Offline");
}
Offline mode means that either the device is not connected or the user has selected
the offline mode from his/her browser toolbar.
Here is how to inform the user that the network is not available and try to reconnect
when a WebSocket close event occurs:
socket.onclose = function (event) {
// Connection closed.
// Firstly, check the reason.
if (event.code != 1000) {
// Error code 1000 means that the
connection was closed normally.
// Try to reconnect.
if (!navigator.onLine) {
alert("You are offline. Please connect to
the Internet and try again.");
}
}
}
The preceding code is pretty simple. It checks the error code to determine whether
the WebSocket connection was closed successfully. Error code 1000 would determ-
ine exactly this. If the close event was raised due to an error, the code would not be
1000. In this case, the code checks for connectivity and informs the user appropri-
ately.
You might notice that this is an HTML5 feature. Later, we will discuss polyfills, so the
following are two polyfills for checking network connectivity:
Search WWH ::




Custom Search