If (Internet == unavailable) in the HTML5 app offiline app

I want to set a condition for the login page if an internet connection is available, then check the server database, otherwise check localstorage.

I tried to verify this with an AJAX call, but when the page is loaded from appcache, it always shows me that I am connected to the Internet. I know a little about the backup part of the manifest file, but I don't know how to use it in my case.

What to do to handle events offline?

+4
source share
1 answer

- Navigator.onLine, .

if (!navigator.onLine) {
   //Browser is offline, pull data from localStorage
} else {
   //Browser is online, pull data through AJAX
}

, :

window.addEventListener('offline', function(e) { 
   //Browser is offline, pull data from localStorage
});

: https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine

IE11, IE Edge, Firefox 51+ Chrome 49+, Safari 10+, iOS 9.3+, Android Browser 4.4+ Chrome Android 57 +


, . . localStorage FALLBACK. localStorage offline.html.

FALLBACK:
/ offline.html

: https://developer.mozilla.org/en-US/docs/Web/HTML/Using_the_application_cache

+7

Source: https://habr.com/ru/post/1675586/


All Articles