Check If Internet Connection Exists in JavaScript


#1

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

#2

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; }}


#3

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:


#4

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


#5

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


#6

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!


#7

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.


#8

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


#9

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:


#10

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!


#11

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


#12

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


#13

Of course, I hope this is what you mean:

index.php (1.5 KB)


#14

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


#15

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.


#16

Hi Kirupa,

I solved the problem with the form , thanks.


#17

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.


#18

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?


#19

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:


#20

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?