0

I want to know if a links redirects to an actual existing image in javascript because I don't want the following to happen:

<img src="http://idontexist.lol/fakeimg.png" alt="non existant image" width="400">

2
  • 1
    use onload function for image to ensure if image exists Commented May 28, 2020 at 17:11
  • You can try hitting the URL and see if it's giving 404 Commented May 28, 2020 at 17:12

1 Answer 1

1

onload() function is used to detect if image is valid in url.If you enter onload function it is valid image url

function test(event,val){
   console.log("image"+val+" is loaded");
   event.style.display="block";
}
<img src="http://idontexist.lol/fakeimg.png"onload="test(this,1)" alt="non existant image"style="display:none" width="400">
<img src="https://cdn.sstatic.net/Img/unified/sprites.svg?v=fcc0ea44ba27g"onload="test(this,2)"style="display:none" alt="non existant image" width="400">

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.