Skip to content

Instantly share code, notes, and snippets.

@alloyking
Created November 27, 2012 14:24
Show Gist options
  • Save alloyking/4154468 to your computer and use it in GitHub Desktop.
Save alloyking/4154468 to your computer and use it in GitHub Desktop.
Check if file exists
function doesFileExist(urlToFile)
{
var xhr = new XMLHttpRequest();
xhr.open('HEAD', urlToFile, false);
xhr.send();
if (xhr.status == "404") {
return false;
} else {
return true;
}
}
////////
var result = doesFileExist("http://www.someplace.com/myimage.png");
if (result == true) {
// yay, file exists!
} else {
// file does not exist!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment