0

I have an image that I am loading from a server. I am doing this through the following code.

img.onload = function () {
   loadingStop = true;
   ctx.drawImage(img, 10, 10);
   startDrawing();
}
img.src = imageLink;

When the image loads the program goes on its merry way. However, incase the photo will not load, I want to alert(msg) the user of the problem. How can I do this with JavaScript?

BMW
  • 35
  • 7
  • 1
    I personally do not know how to do this however hope this helps: https://stackoverflow.com/questions/9815762/detect-when-an-image-fails-to-load-in-javascript –  Jul 23 '20 at 17:36

1 Answers1

1

Just use onerror

<img src="../missing-file.jpg" onerror="myAlertFunction()">
function myAlertFunction() {
  alert('The image could not be loaded.');
}
wizAmit
  • 310
  • 1
  • 8