3

Possible Duplicate:
JavaScript: How to detect that the internet connection is offline?

How to check the internet is connected or not in javascript.

Community
  • 1
  • 1
Niger
  • 3,916
  • 5
  • 30
  • 30
  • What if the Internet was connected, but you could not reach the web servers you needed to reach? It wouldn't help you much to know that you were connected to the Internet in that case. – John Saunders Aug 15 '09 at 02:22
  • Duplicate: http://stackoverflow.com/questions/189430/javascript-how-to-detect-that-the-internet-connection-is-offline/189456 – Charlie Salts Aug 15 '09 at 02:23
  • That is actually a better way to think. It isn't that the client is offline, it is just that they app can't reach the servers. – Tyler Carter Aug 15 '09 at 02:23
  • I mean, you can think about it either way in the end. You just have to be clear what you define as 'offline', and tell that to the user. – Tyler Carter Aug 15 '09 at 02:33

1 Answers1

0

You can try to access a web page in Ajax every selected period of time. This code will help you:

http://www.codeproject.com/KB/scripting/InternetConnectionTest.aspx

It is very useful, and because it is Ajax based, is asyncronous, so you can check if the Internet is conected without leaving the original page.

For your convenience try to access a very fast page from a site that is always online (i.e. Google)

backslash17
  • 5,300
  • 4
  • 31
  • 46
  • You can't access sites outside of your domain (i.e. Google) with Ajax because of Javascript's same origin policy. – jimyi Aug 15 '09 at 02:56
  • You're right ,sorry! I forgot the javascript policy – backslash17 Aug 15 '09 at 16:20
  • But the script works anyway, because at the end you want to know if the user has or not access to the pages you want to serve. Is not relevant if the problem is in the connection or the server, the fact is that there is or not access. – backslash17 Aug 15 '09 at 16:24