1

I want to create jquery listener to check internet connectivity.

    function checkInternetConnection()
    {
        onConnectionClosed:function(){
            dialog.show("Connection closed please wait");
        }
        onConnectionOpened:function(){
            dialog.hide();
        }
    }
    
    $(document).ready(function(){
        checkInternetConnection();
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

So, when the connection is turned off, I want to show a dialog to user. When the connection is back I want to hide the dialog. How can I do this?

4
  • It's not possible, as a workaround however you could make a dummy request to an api and see if it fails (check which reason).
    – Adrian
    Commented Jul 17, 2017 at 13:35
  • @NickShvelidze Support for Navigator#connection is not supported by all browsers. Should be avoided.
    – Adrian
    Commented Jul 17, 2017 at 13:36
  • Should be avoided if your target is all browsers...
    – user736893
    Commented Jul 17, 2017 at 13:39

1 Answer 1

5

this is a snippet from MDN:

https://developer.mozilla.org/en-US/docs/Online_and_offline_events

window.addEventListener('load', function() {

  function updateOnlineStatus(event) {
    var condition = navigator.onLine ? "online" : "offline";

    alert(`you are ${condition}`)

  }

  window.addEventListener('online',  updateOnlineStatus);
  window.addEventListener('offline', updateOnlineStatus);
});
2
  • Worth noting for OP that it doesn't support Opera.
    – Adrian
    Commented Jul 17, 2017 at 13:37
  • I want to use it in chrome sir Commented Jul 17, 2017 at 13:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.