JSFiddle - React, Tailwind, and code Playground
HTML
<button type="button">Testar</button>
JavaScript
function testaLigacao(callback) {
var xhr = new XMLHttpRequest();
var url = "http://sergiofrilans.se";
var novoNumero = new Date().getTime();
xhr.open('HEAD', url + "?rand=" + novoNumero, true);
xhr.onreadystatechange = function () {
if (xhr.readyState != 4) return;
if (xhr.status >= 200 && xhr.status < 304) return callback(true);
return callback(false);
}
try {
xhr.send();
} catch (e) {
return callback(false);
}
}
document.querySelector('button').addEventListener('click', function () {
testaLigacao(function (online) {
alert('O browser está ' + (online ? 'online.' : 'offline.'));
});
});