Skip to content

Instantly share code, notes, and snippets.

@kllaudyo
Last active August 20, 2018 13:50
Show Gist options
  • Save kllaudyo/d0b40da890d6d51ace1ba8ec5c0922c2 to your computer and use it in GitHub Desktop.
Save kllaudyo/d0b40da890d6d51ace1ba8ec5c0922c2 to your computer and use it in GitHub Desktop.
Verify internet connection
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;
};
@kllaudyo
Copy link
Author

Segundo commit atendendo ao pedido do meu amigo Marcelo "Murchila"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment