From http://www.jpsilvashy.com/posts/check-connection-with-javascript.html
https://gist.github.com/jpsilvashy/5725579