OnlineJS – lightweight and easy to use library to check your internet connection status and react on it
Detecting internet connection status with JavaScript is generic problem during mobile web applications or dynamic sites development and everybody solves it in his own way. Online JS is a compilation of best practices for detecting connection status, it`s reliable, fast, very lightweight and compatible with all popular frameworks library that is very easy to use.
Include this library into your project and just check is window.onLine === true
Define window.onLineHandler
or 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.