From http://www.jpsilvashy.com/posts/check-connection-with-javascript.html