How to Check Internet Connection Exists or Not using Javascript?

Last modified on by Karthikeyan K

99% of people used to type immediately after opening their web browser to check internet connection. This is very true in our morden world. Now a days we can’t live without an Internet. While browing, If you lose the connection then you will feel sad and unhappy till the connection is UP.

In this tutorial, I’m going to explain how to check Internet connection exists or not by using pure Javascript plugin called  offline.js . This javascript plugin will notify the user whenever they lost the internet connection like Gmail. Check my previous popular post on – Gmail Style Inbox Design using jQuery & CSS

By using simple ajax call, it tell us the internet connection is up or down. Also it notify them when the connection is up.

How to Check Internet Connection Exists or Not using Javascript


How it Works?

It keeps on sending Ajax request to load an Image (1 byte in size) to check the connection. If that ajax call receives 200 Ok status code, then internet connection is UP. If it returns 204 No Content status code, then Internet connection is Down.

Please place the below javascript before  </body>  tag

<script src=""></script>

Load the below stylesheet before  </head>  tag. 1st css is for styling the theme. 2nd css is for language content.

<link rel="stylesheet" type="text/css" href="">
<link rel="stylesheet" href="" />

Now please this javascript before  </body>  tag.

Offline.options = {checks: {xhr: {url: '/tiny-image.gif'}}};

You should replace your image with less in size. or download this image –

Check Internet Connection with Pure Javascript

Please check the live demo to see how it is working beautifully. With few lines of code and one single tiny image can help you to make this feature go live in a minute.

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *