and if you're interested in the actual mechanics of detecting online / offline connection, look into navigator.onLine and "online" and "offline" events. https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOn...
(We also use polling as a backup for browsers that don't support the above features)