3

I have a web application that is running in an environment on mobile devices that looses internet connectivity frequently. I want to take different actions using javascript depending on whether the wireless network is down vs when the internet is down (such as what error to log).

I can make an ajax service call to my server and if I can reach it I know that both the wifi and internet are working, but if this call fails I am not certain how to distinguish if it is because of a network or internet issue.

Does anyone know how to check if a device is connected to a wifi network using javascript? I do not need the SSID or name of the network, just that it is actually connected to something.

lehn0058
  • 19,977
  • 15
  • 69
  • 109
  • 3
    `navigator.onLine`: https://developer.mozilla.org/en-US/docs/Web/API/window.navigator.onLine – Ian Aug 09 '13 at 15:36
  • 2
    Note: If the browser doesn't support navigator.onLine the above example will always come out as falsy (false/undefined) – Srikanth AD Aug 09 '13 at 15:38
  • 3
    There is an article linked from the `navigator.onLine` doc that offers several complimentary techniques: http://www.html5rocks.com/en/mobile/workingoffthegrid/ The gist of the article is: online vs. offline is complicated. – Ross Allen Aug 09 '13 at 15:42
  • @ssorallen, from that article it sounds like the navigator.onLine is true only if the browser has internet connectivity. So, that means this would tell me the same result as my service call (which is whether or not my device has internet connectivity) but not whether or not it is connected to a wireless network. Does that sound correct? – lehn0058 Aug 09 '13 at 15:50
  • I think the navigator.onLine doesn't help me determine if I am connected to a wireless network, which is the part that I am still missing. – lehn0058 Aug 09 '13 at 15:54
  • Actually, my tests show the opposite. I created a firewall rule to block all outbound traffic from chrome, and the navigator.onLine still returned true. It only returned false when I disabled my network card. I think this will be able to tell me what I want! – lehn0058 Aug 09 '13 at 16:28
  • `navigator.onLine` does not expose details about the internet connection, like whether it is a wired or wireless connection. Those details are not something that should be exposed by the browser. – Ross Allen Aug 09 '13 at 17:33
  • Correct - in my case all of my clients are wireless though, so if they have network connectivity I can assume they have a wifi connection. – lehn0058 Aug 09 '13 at 17:46

1 Answers1

0

this feature sounds bleeding edge.

so, if you merge information ping from your server, and the connection type you can determine when are you really online and when are you just connected over Wi-Fi.

but first, check this out:

How do I check connection type (WiFi/LAN/WWAN) using HTML5/JavaScript?

Community
  • 1
  • 1