1

I'm trying to check if an image url is valid before setting in my img src. At the moment this is what I have:

function checkURL(url) {
    var img = new Image();
    
    img.onerror = function () {
        return false;
    }

    img.onload = function () {
        return true;
    }

    img.src = url;
}

In both cases is returning undefined instead of true/false.

Here is a fiddle

What can I do to solve my issue? I'm using jQuery 3.6.0.

User1899289003
  • 850
  • 2
  • 21
  • 40
  • @jeremy-denis I'm checking the answers in that question to see if they help me but it seems that not, with this code I'm not trying to remove/add some src. I just want to check if the url is valid and return true/false. – User1899289003 Mar 03 '22 at 07:08
  • Could you check this example? https://jsfiddle.net/pgecw52q/4/ – Maniraj Murugan Mar 03 '22 at 07:41

0 Answers0