check whether image exists javascript code example
Example 1: javascript how to check if image exists
// create an XHR object
const xhr = new XMLHttpRequest();
// listen for `onload` event
xhr.onload = () => {
if (xhr.status == 200) {
console.log('Image exists.');
} else {
console.log('Image does not exist.');
}
};
// create a `HEAD` request
xhr.open('HEAD', '/img/bulb.svg');
// send request
xhr.send();
Example 2: javascript check if is image
function isFileImage(file) {
return file && file['type'].split('/')[0] === 'image';
}