10

What is the best way to detect if there's internet connection or not on my mobile via my web app?

Charles
  • 50,943
  • 13
  • 104
  • 142
Satch3000
  • 47,356
  • 86
  • 216
  • 346

5 Answers5

22

There's no code necessary for this -- it's part of the HTML5 API. Check the value of window.navigator.onLine -- it will be false if the user is offline.

http://www.whatwg.org/specs/web-apps/current-work/multipage/offline.html#browser-state

Liza Daly
  • 2,933
  • 23
  • 31
  • 2
    This give me status as online even if I am offline. `

    The network is: (state unknown) ` I tried to do formatting but could not :(

    – Anuj Verma Nov 26 '12 at 16:13
  • try this : function hasConnection() { if (window.navigator.connection.type === Connection.NONE) { return false; } return true; } – Mimouni Aug 11 '15 at 11:40
4

An option might be changing the Ajax settings to add a specific timeout, then add an error handler that looks for a textStatus (second argument) of 'timeout'.

When a timeout occurs, either internet connectivity is spotty or your site is down.


Using ajaxSetup to set option defaults for all requests:

$.ajaxSetup({
    timeout: 1, // Microseconds, for the laughs.  Guaranteed timeout.
    error: function(request, status, maybe_an_exception_object) {
        if(status != 'timeout')
            alert("YOU BROKE IT");
        else
            alert("OH NOES TEH INTARWEBS ARE DOWN!!!!!1one");
    }
});
Charles
  • 50,943
  • 13
  • 104
  • 142
  • @Satch, I have used the links I provided to assemble a functional but not-too-useful example for you. **I highly suggest referring to the documentation if you wish to learn more**, as everything I did here was based purely on the documentation and the description I provided of the task required. – Charles Apr 11 '11 at 21:15
  • This doesn't really work. The answer with window.navigator.onLine is the correct one. – DATEx2 Oct 23 '12 at 14:41
  • @Satch3000 can you show me any working example? or how can we implement centrally? – Rahul Rajput Dec 19 '13 at 06:00
2

in simple JS code this can done, causation all featured devices not have JS supported however for web-based application this is very minimum code to use

online = window.navigator.onLine;
if (navigator.onLine) {
  alert('you are online');
} else {
  alert('you are offline');
}
1

if you want to check every X seconds the connection.

        $(document).ready(function() {
            setInterval(function(){
                var isOnline = navigator.onLine;
                if (isOnline) {
                    console.log("Connected");
                }
                else {
                    console.log("Not Connected");
                }
            }, 30000); // 10000 = 10 seconds, check for connection every 30 seconds
        });
-1

If you want something with more compatibility, reliability and customisability than window.navigator.onLine, try my jQuery plugin: http://tomriley.net/blog/archives/111

tripRev
  • 830
  • 3
  • 12
  • 27