Since @senocular picks on me for using xhr instead of Fetch, I figured Iād post a solution that uses the newer Fetch API instead
fetch("https://www.kirupa.com/book/images/learning_react2.gif", {
method: "head",
mode: "no-cors"
})
.then(function(response) {
if (response.status == 200) {
console.log("file exists!");
} else {
console.log("file doesn't exist!");
}
return response.text();
})
.catch(function(error) {
console.log("Error ", error);
});
The result should be nearly identical!
Cheers,
Kirupa