Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
Java script function to display a message in place of image if image source is not found and image is not loaded

I am working on a project in which I will be getting HTML file from another source and it can have multiple images, so I cannot modify HTML file. I am planning to write a java script in order to solve below issue.

But that file has some img sources which are not present so image is not getting loaded.

I want to display a particular message like "Please login in order to view this image" instead of unloaded image if it is not loaded while if other images are loaded they should be shown as it is.

It will be helpful if someone can provide any feedback.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Here is how to test after the page has loaded

window.addEventListener("load", event => {
  document.querySelectorAll('img').forEach(img => {
    const isLoaded = img.complete && img.naturalHeight !== 0;
    if (!isLoaded) img.outerHTML = "Please login in order to view this image"
  })
})
<img src="image1.jpg" /><br/>
<img src="image2.jpg" /><br/>
<img src="image3.jpg" /><br/>
<img src="image4.jpg" /><br/>
<img src="image5.jpg" /><br/>
<img src="image6.jpg" /><br/>

about 4 years ago · Juan Pablo Isaza Report

0

If I correctly understand what your're asking, you can do this by using alt within the HTML tag. For example:

<img src="computer.jpg" alt="Computer Image" width="500" height="600">

If you run that snippet, you can see that since the image is not defined, text will appear.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!