Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting Internet Connection with JavaScript

Currently, I'm making a Cached iPhone-app with some features that require internet, and other features that don't. I'd like to know how to detect an internet connection with JavaScript, to make certain pages display a 'No Internet Connection' text, and others to work as normal.

like image 334
George Peters Avatar asked May 08 '12 23:05

George Peters


People also ask

How do I know if JavaScript is connected to internet?

JavaScript has a function called navigator. This browser property can be used to determine if you are connected to the internet. It returns true or false depending on the status of the client's network and says whether the browser is online or offline easily.

How do I know if someone is using my Internet connection?

Select the Start button, then type settings. Select Settings > Network & internet. The status of your network connection will appear at the top.

Which code checks if the browser is online?

onLine. Returns the online status of the browser.


1 Answers

In HTML5, you can use:

 navigator.onLine

to detect internet connectivity.

LIVE DEMO: http://jsfiddle.net/DerekL/fHQK4/

like image 71
Derek 朕會功夫 Avatar answered Nov 15 '22 07:11

Derek 朕會功夫