This is a way to detect that a connection to the server failed, not that the browser is offline. If that's good enough for you, most libraries have a way to set a handler for all failed Ajax requests. However, navigator.online is the property that tells you that you are connected to the internet.
window.navigator.onLine only tells if the browser is set to offline -mode (at least in Firefox). If the Internet connection has been lost, it will still show true, for me at least.
navigator.onLine works well in Chrome (5 years later). I would recommend using that unless you have a very specific scenario.