Detect the Internet connection is offline or online using JavaScript

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

🙋‍♂️ Shubham Verma    🗓 January 29, 2021


Detect the Internet connection is offline or online using JavaScript


How to 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

Detect the Internet connection is offline or Online using JavaScript



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.



Support our IDKBlogs team

Creating quality content takes time and resources, and we are committed to providing value to our readers. If you find my articles helpful or informative, please consider supporting us financially.

Any amount (10, 20, 50, 100, ....), no matter how small, will help us continue to produce high-quality content.

Thank you for your support!




Thank you

I appreciate you taking the time to read this article. The more that you read, the more things you will know. The more that you learn, the more places you'll go. 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.
To read more about the technologies, Please subscribe us, You'll get the monthly newsletter having all the published article of the last month.