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 http://www.kirupa.com/html5/check_if_internet_connection_exists_in_javascript.htm

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

function doesConnectionExist() { var xhr = new XMLHttpRequest(); var file = ā€œhttp://www.yoursite.com/somefile.pngā€; var randomNum = Math.round(Math.random() * 10000); xhr.open(ā€˜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 ASP.net 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: https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/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.

Thanks

btw I am an absolute beginnerā€¦obviously

This is the test url: http://coolsoftwaredesign.com/apptrying/test.htm

Youā€™ve hit upon an interesting issue! If you change your pageā€™s URL by adding a ā€œwwwā€, the example works: http://www.coolsoftwaredesign.com/apptrying/test.htm

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!

Hi,

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): www.coolsoftwaredesign.com/apptrying/index.php

I hope you can solve my dilemma.

Thanks in advance.
Erwin

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: https://www.kirupa.com/html5/checking_if_a_file_exists.htm

:slight_smile:

Hi,
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?