Detect the Internet connection is offline or Online using JavaScript

How to detect that you are connected with internet or not using JavaScript?

  idkblogs.com      January 29, 2021

Shubham Verma

Shubham Verma


Detect the Internet connection is offline or Online using JavaScript

D

etecting the network connectivity is easy in JavaScript. Sometime it is required in our app to determine the network connetion to perform any connectivity tasks. In the javascript we eaisly find out the status of the internet connection. Sometimes, developers use AJAX or some other method like server call to detect the internet connection, which is not good and as per the standard, because its need some bandwidth and takes some time to detect the internet connection of your machine. Here we'll see the best way to detect the internet connection of your machine using JavaScript.

There is a property to detect the internet connection in javascript ie: navigator.onLine isBrowserOnline() So, here, in Javascript, we can make a function that will return the status of the internet connection of your machine. Let's create a function name isBrowserOnline(). It will return a boolean value, True for online and False for offline.



Here, we'll make a HTML page where would be a button to check the status of internet connection. after clicking on "Check Status" button, it will display the status of your internet connection.

So let's dive into the codes:

connection.html:


Output:
Detect the Internet connection is offline or Online using JavaScript

Source: idkblogs.com



Using the above code, you can get the network connection (internet connection) status in your browser or app.

Conclusion:


In the javascript, detecting the network connection (internet connection) is very easy, you need to use "navigator.onLine" property and this will tell you about the network connection/internet connection.


Thank you

for taking the time to read this article. If you’re interested in Node.js or JavaScript this link will help you a lot.

If you found this article is helpful, then please share this article's link to your friends to whom this is required, you can share this to your technical social media groups also. You can follow us on our social media page for more updates and latest article updates.