In some cases, it is necessary to determine whether the browser is online or offline before performing a required task. Many developers use AJAX to determine the connection status of the browser (online or offline) by sending a request to the server. However, this is not a good method for determining the state of the browser because it requires bandwidth and can also affect usability.
Yet JavaScript’s Browser Object Model(BOM) provides a direct way to detect browser’s connectivity status i.e. whether the browser is online or offline.
To perform this check, targeting all possible browsers out there, we will be using the following property :
navigator.onLine
Syntax:
function isOnline() {
return ( navigator.onLine)
}
Example: This example displays a button if it is clicked, It will show the connectivity status.
<!DOCTYPE html> <html> <body> <p>Click the button to check if the browser is online.</p> <button onclick="isOnline()"> Click Me </button> <p id="demo"></p> <script> function isOnline() { if (navigator.onLine) { document.getElementById( "demo").innerHTML = "Online"; } else { document.getElementById( "demo").innerHTML = "Offline"; } } </script> </body> </html> |
Note: The minimum version of browsers that supports the property:
- Google Chrome: 14.0
- Internet Explorer: Yes
- Firefox: 3.5
- Safari: 5.0.4
- Opera: Yes
Output:

Recommended Posts:
- Service Worker | How to create custom offline page ?
- How to auto-save data when application is offline in Angular 8?
- Detect a device is iOS or not using JavaScript
- How to detect flash is installed or not using JavaScript ?
- How to detect HTML 5 <canvas> is not supported by JavaScript ?
- Database Connection and Queries in Codeigniter
- HTTP headers | Connection
- Laravel | MySQL Database Connection
- How to check the user is using Internet Explorer in JavaScript?
- How to detect touch screen device using JavaScript?
- Detect the Operating System of User using JavaScript
- How to detect the device is an Android device using JavaScript ?
- How to detect operating system on the client machine using JavaScript ?
- How to detect the user browser ( Safari, Chrome, IE, Firefox and Opera ) using JavaScript ?
- How to detect when the window size is resized using JavaScript ?
- How to detect If textbox content has changed using JavaScript ?
- How to detect whether a device is iOS without using Regular Expression in JavaScript?
- How to detect when cancel is clicked on file input using JavaScript ?
- How to detect network speed using JavaScript?
- How to detect page zoom level in all modern browsers using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


