check if img src exists code example
Example: 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();