OnlineJS – lightweight and easy to use library to check your internet connection status and react on it
Include this library into your project and just check is
window.onLine === true
window.offLineHandler functions to handle status changes.
Library uses such optimizations:
- checks status in background mode,
- requests only headers to minimize traffic,
- listens for window.online/offline events and verifies them.
P.S. I`m often asked: “Why library does not use
navigator.onLine ?”. This property is a bit underhandled and inconsistent between different browsers. For many of them it only shows status of local network connection and for some versions of FireFox it depends only on autonomic mode settings. So using this property in critical tasks is not good idea. Online JS is a right way for serious project because library uses navigator.onLine only as one of possible triggers and then makes more tests of internet connection status. You can rely on Online JS.