javascript检测网络连接是否正常
function doesConnectionExist(file) {
var xhr = new XMLHttpRequest();
var randomNum = Math.round(Math.random() * 10000);
xhr.open('HEAD', file + "?rand=" + randomNum, false);
try {
xhr.send();
if (xhr.status >= 200 && xhr.status < 304) {
return true;
} else {
return false;
}
} catch (e) {
return false;
}
}
doesConnectionExist("http://www.yoursite.com/somefile.png");