Detect the Internet connection is offline?

后端 未结 19 1380
甜味超标
甜味超标 2020-11-22 00:53

How to detect the Internet connection is offline in JavaScript?

19条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 01:38

    an ajax call to your domain is the easiest way to detect if you are offline

    $.ajax({
          type: "HEAD",
          url: document.location.pathname + "?param=" + new Date(),
          error: function() { return false; },
          success: function() { return true; }
       });
    

    this is just to give you the concept, it should be improved.

    E.g. error=404 should still mean that you online

提交回复
热议问题