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

182
Views
Prevent show not fully loaded images

I have a page for showing users' data, like their images (Personal, Learning, etc).

For showing images, I have a Modal. it has an img tag.

Manager of the website, clicks on links, and website opens a Modal for him.

But there is only 1 Modal and one img tag!

Manager clicks on Personal Picture of one user. Browser loads the picture and shows it into the Modal.

Manager closes the modal and opens Bachelor's degree photo of the same user. Browser first opens the same Modal (containing previous picture) and then loads the new picture!!!

Manager sees the previous picture for a few moments and I think it will make a bad experience for all users, Especially the users, whom have a slow network.

Modal, opens by JS. So how can I make sure that picture has loaded to open the Modal after that?

I really need it. Is there any idea?

(I use jQuery too.)

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

0

One way would be to give them a class that gives them opacity: 0 so they don't show:

<img src="/path/to/image" class="loading">

And in CSS:

.loading {
opacity: 0;
}

In head, we override that if JavaScript is disabled (so we're not unfriendly to non-JavaScript visitors :

<noscript>
<style>
.loading {
    opacity: 1;
}
</style>
</noscript>

...and then in code at the bottom of your page, find all your images and remove the class when they've loaded

    (function() {
    // Get an array of the images
    var images = Array.prototype.slice.call(document.querySelectorAll("img.loading"));
    // Hook their load and error events, even though they may have already fired
    images.forEach(function(image) {
        image.addEventListener("load", imageDone.bind(null, image));
        image.addEventListener("error", imageDone.bind(null, image)); // Could handle errors differently
    });
    // Check to see if any images are already complete
    checkImages();

    function imageDone(img) {
        img.classList.loading("remove");
        images = images.filter(function(entry) { entry != img });
    }
    function checkImages() {
        images.forEach(function(image) {
            if (image.complete) {
                imageDone(image);
            }
        });
        if (images.length) {
            // Check back in a second
            setTimeout(checkImages, 1000);
        }
    }
})();

That's a belt-and-braces approach. It proactively checks to see if images have finished loading, and also reactively handles the load and error event of images. In theory, we shouldn't need the setTimeout, and you might do testing without it, but...

Notice how once an image is complete, we remove the class so it's visible.

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!