How to check if the user is online using javascript or any library?

前端 未结 10 1734
你的背包
你的背包 2020-12-03 01:35

I need some help on how I could check the internet connection using Javascript or jQuery or any library if available. cause i\'m developing an offline appl

10条回答
  •  清歌不尽
    2020-12-03 01:52

    navigator.onLine is a property that maintains a true/false value (true for online, false for offline). This property is updated whenever the user switches into "Offline Mode".

    window.addEventListener('load', function() {
    
      function updateOnlineStatus(event) {
         document.body.setAttribute("data-online", navigator.onLine);
      }
      updateOnlineStatus();
      window.addEventListener('online',  updateOnlineStatus);
      window.addEventListener('offline', updateOnlineStatus);
    });
    

提交回复
热议问题