# Check If Internet Connection Exists in JavaScript

**URL:** <https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214>\
**Category:** programming\
**Created:** [November 23, 2014, 2:09am UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214 "2014-11-23T02:09:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [November 23, 2014, 2:09am UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/1 "2014-11-23T02:09:49Z")

</div>

by [kirupa](http://www.kirupa.com/me/index.htm) | 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](http://www.kirupa.com/html5/check_if_internet_connection_exists_in_javascript.htm)

---

<div class="post-metadata">

**Author:** ![deepak1990](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@deepak1990](https://forum.kirupa.com/u/deepak1990)\
**Post date:** [December 31, 2014, 11:30am UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/2 "2014-12-31T11:30:45Z")

</div>

hi guys i m using this code for check internet connection in [asp.net](http://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](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; }}

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [December 31, 2014, 8:31pm UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/3 "2014-12-31T20:31:49Z")

</div>

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](http://ASP.net) app 🙂

---

<div class="post-metadata">

**Author:** ![Rishi1231](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Rishi1231](https://forum.kirupa.com/u/Rishi1231)\
**Post date:** [February 26, 2015, 3:31pm UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/4 "2015-02-26T15:31:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [February 26, 2015, 5:39pm UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/5 "2015-02-26T17:39:51Z")

</div>

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

For the timer, take a look at setInterval: [https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval)

---

<div class="post-metadata">

**Author:** ![Rishi1231](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Rishi1231](https://forum.kirupa.com/u/Rishi1231)\
**Post date:** [February 26, 2015, 6:20pm UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/6 "2015-02-26T18:20:33Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [February 27, 2015, 4:19am UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/7 "2015-02-27T04:19:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![erwin007](https://avatars.discourse-cdn.com/v4/letter/e/ba9def/32.png) [@erwin007](https://forum.kirupa.com/u/erwin007)\
**Post date:** [March 17, 2016, 2:24am UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/8 "2016-03-17T02:24:15Z")

</div>

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](http://coolsoftwaredesign.com/apptrying/test.htm)

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [March 17, 2016, 8:52pm UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/9 "2016-03-17T20:52:10Z")

</div>

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](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 🙂

---

<div class="post-metadata">

**Author:** ![erwin007](https://avatars.discourse-cdn.com/v4/letter/e/ba9def/32.png) [@erwin007](https://forum.kirupa.com/u/erwin007)\
**Post date:** [March 18, 2016, 7:45am UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/10 "2016-03-18T07:45:50Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![erwin007](https://avatars.discourse-cdn.com/v4/letter/e/ba9def/32.png) [@erwin007](https://forum.kirupa.com/u/erwin007)\
**Post date:** [March 18, 2016, 1:52pm UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/11 "2016-03-18T13:52:17Z")

</div>

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

I hope you can solve my dilemma.

Thanks in advance.  
Erwin

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [March 18, 2016, 7:24pm UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/12 "2016-03-18T19:24:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![erwin007](https://avatars.discourse-cdn.com/v4/letter/e/ba9def/32.png) [@erwin007](https://forum.kirupa.com/u/erwin007)\
**Post date:** [March 18, 2016, 7:54pm UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/13 "2016-03-18T19:54:46Z")

</div>

Of course, I hope this is what you mean:

[index.php](https://forum.kirupa.com/uploads/short-url/fVtOxbjScZWwGReGCYM3YJyNUpU.php) (1.5 KB)

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [March 19, 2016, 1:25am UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/14 "2016-03-19T01:25:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [March 19, 2016, 5:02am UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/15 "2016-03-19T05:02:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![erwin007](https://avatars.discourse-cdn.com/v4/letter/e/ba9def/32.png) [@erwin007](https://forum.kirupa.com/u/erwin007)\
**Post date:** [March 21, 2016, 4:53pm UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/16 "2016-03-21T16:53:06Z")

</div>

Hi Kirupa,

I solved the problem with the form , thanks.

---

<div class="post-metadata">

**Author:** ![benedettoapp](https://avatars.discourse-cdn.com/v4/letter/b/e274bd/32.png) [@benedettoapp](https://forum.kirupa.com/u/benedettoapp)\
**Post date:** [October 21, 2016, 8:54am UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/17 "2016-10-21T08:54:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jster](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jster](https://forum.kirupa.com/u/jster)\
**Post date:** [July 23, 2018, 9:41pm UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/18 "2018-07-23T21:41:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [July 23, 2018, 11:27pm UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/19 "2018-07-23T23:27:10Z")

</div>

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

🙂

---

<div class="post-metadata">

**Author:** ![Crazzy\_MindBoggler](https://avatars.discourse-cdn.com/v4/letter/c/edb3f5/32.png) [@Crazzy\_MindBoggler](https://forum.kirupa.com/u/Crazzy_MindBoggler)\
**Post date:** [August 6, 2018, 10:25am UTC](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214/20 "2018-08-06T10:25:47Z")

</div>

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?

[Next page](https://forum.kirupa.com/t/check-if-internet-connection-exists-in-javascript/352214.md?page=2)
