Last active
August 20, 2018 13:50
-
-
Save kllaudyo/d0b40da890d6d51ace1ba8ec5c0922c2 to your computer and use it in GitHub Desktop.
Verify internet connection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const HTTP_STATUS_OK = 200, | |
HTTP_STATUS_NOT_MODIFIED = 304, | |
HTTP_STATUS_MULTIPLE_CHOICE = 300, | |
HTTP_METHOD = "HEAD", | |
hasConnection = (host = window.location.protocol + "//" + window.location.hostname) => { | |
if(window.navigator) | |
return navigator.onLine; | |
else{ | |
const xhr = new (window.ActiveXObject || XMLHttpRequest)("Microsoft.XMLHTTP"); | |
xhr.open(HTTP_METHOD, host, false); | |
try{ | |
xhr.send(); | |
return (xhr.status >= HTTP_STATUS_OK && (xhr.status < HTTP_STATUS_MULTIPLE_CHOICE || xhr.status === HTTP_STATUS_NOT_MODIFIED)); | |
}catch(e){ | |
return false; | |
} | |
} | |
return false; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Segundo commit atendendo ao pedido do meu amigo Marcelo "Murchila"