Check If Internet Connection Exists in JavaScript

by kirupa | 14 September 2013

If there is ever a moment when you need your internet connection to work, there is a very good chance it is at that very moment your internet connection will fail. This is one of those universally accepted truths...I think. Anyway, building an app or a site whose functionality relies on an internet connection being present is tricky. You can't always guarantee that an internet connection will be available when your app requires it, and this is especially true on mobile devices when your internet connectivity might be flaky.

This is a companion discussion topic for the original entry at

hi guys i m using this code for check internet connection in c# but it doesn’t work do you have any solution.

function doesConnectionExist() { var xhr = new XMLHttpRequest(); var file = “”; var randomNum = Math.round(Math.random() * 10000);‘HEAD’, file + “?rand=” + randomNum, false); try { xhr.send(); if (xhr.status >= 200 && xhr.status < 304) { return true; } else { return false; } } catch (e) { return false; }}

Can you set a breakpoint and see where it is failing? This code is JS, but it shouldn’t have any problems working on the browser side of your app :slight_smile:

Hi I am running this script on client side and always getting the return value as false. I am actually using JS code in SmartGWT using JSNI but it always return false. Can you also help me with the timer so that this is checked frequently as I really need it and I would really appreciate your help

I am not very familiar with SmartGWT or JSNI, so I can’t help you out much there :frowning:

For the timer, take a look at setInterval:

Ok can you tell me how can this function call some other domain to verify for Internet Connection. I mean cross-domain connectivity in Javascript. Thanks in advance!

Security restrictions will prevent you from calling JS code on another server directly. There are some tricks you can use. Before I consider mentioning them, I’m assuming you can’t modify (or add) code on the other server.

Hi, I am trying this code but it doesn’t work.
Of course it’s my fault but after 2 days of trying I am desperate…

I run it on a website so not locally but it always gives me Connection doesn’t exists while it does for sure.

Please help.


btw I am an absolute beginner…obviously

This is the test url:

You’ve hit upon an interesting issue! If you change your page’s URL by adding a “www”, the example works:

If you don’t use the www regularly for referring to your site, just remove it from the URL of the image file in your code :slightly_smiling:

Hi Kirupa,

You are a life-saver man!
Thank you so much.

It’s weird it needs the www but then again if it works it’s ok with me!


The internet-connection test works but I need to have it checked in a form when the Send-button is clicked before sending the email and I have no clue.
I tried a couple of things but all without result.

Here you can see the files (index.php, thankyou.html):

I hope you can solve my dilemma.

Thanks in advance.

Can you post the code you are using when the Send button is clicked? :slight_smile:

Of course, I hope this is what you mean:

index.php (1.5 KB)

Yikes! I was afraid of that. I am not as familiar with PHP to know what to do there unfortunately :frowning:

I feel like this is a non-issue. If PHP is executing any code at all, that means it’s just received a request (99.99% likely over the internet) from a browser. If erwin007 is talking about some crazy setup where there’s some super weird LAN restrictions and the client and server are basically co-located, then that should be clarified.

OTOH, maybe erwin007 just wants to know that $('#myForm').on('submit', async e => await checkInternet() ? true : e.preventDefault()) is a thing.

Hi Kirupa,

I solved the problem with the form , thanks.

I don’t know java, so even a simple thing becomes impossible for me, what i would like to do is to use this code on a simple link, if internet exists open the link if not show a message, is there some example of this somewhere, i was not able to find. Second i would like to do the same for Iframes if internet exists show the contents of the iframe, if not show message.

Hey all, so would this code work if I also wanted to check to see if an IP address is connected to a network? I want to see if an IP address is online or offline?

Unfortunately, no. There is no way in JS to ping a particular IP address and read the result. Is that IP address a web server? If so, you could check for the existence of a file on it as an indirect way of checking whether that IP address is connected to the network. This tutorial walks through how you can do that:


Just wanted to check, will it execute at a certain period of time?
Suppose when i start my application the internet connection is there, but then in between the internet connection is lost in that case will the readyStateChange event auto fires?