Skip to content

Instantly share code, notes, and snippets.

@stevermeister
Created November 27, 2014 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevermeister/3fe51678fef2e5eacc9f to your computer and use it in GitHub Desktop.
Save stevermeister/3fe51678fef2e5eacc9f to your computer and use it in GitHub Desktop.
Check connection
function doesConnectionExist() {
var xhr = new XMLHttpRequest();
var file = "http://www.yoursite.com/somefile.png";
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment