How to Load an Image Asynchronously with JavaScript

Andrew Chalkley

May 20, 2022

-

3 min read

Learn

Last Updated on March 4, 2026 by Laura Coronel

Sometimes you want images to load quietly in the background instead of appearing progressively on the page. This is common in image galleries, games, and interfaces where you want to control when an image appears—only after it has fully downloaded.

JavaScript makes this easy by allowing you to preload images asynchronously and swap them into the page once they’re ready.

In this article, you’ll learn a simple, reliable pattern for loading images asynchronously using JavaScript.


Overview

The basic process for loading an image asynchronously looks like this:

  1. Create a new Image object in JavaScript
  2. Assign a URL to its src attribute to start downloading
  3. Listen for the load event
  4. Replace the visible image once loading completes

This approach works in all modern browsers and doesn’t require any external libraries.


Setup

1. HTML

Start with an <img> element that acts as the destination for your final image.

<img id="mainImage" src="placeholder.png" alt="Loading image">

The placeholder image can be:

  • A transparent image
  • A low-resolution preview
  • Or a neutral placeholder graphic

This prevents broken-image icons while the real image loads.


2. CSS

You can optionally show a loading indicator using CSS.

img {
  width: 600px;
  height: 450px;
  background: url("loading.gif") center no-repeat;
  border: 1px solid #000;
  border-radius: 5px;
}

This keeps the layout stable and gives users visual feedback while the image downloads.


Loading an Image with JavaScript

First, get a reference to the destination image element.

const image = document.getElementById("mainImage");

Next, create a new Image object. This image will load in memory without affecting the page.

const downloadingImage = new Image();

Before assigning a source, attach a load event handler. This ensures the image is fully downloaded before it’s displayed.

downloadingImage.onload = () => {
  image.src = downloadingImage.src;
};

Finally, assign the image URL to start the download.

downloadingImage.src = "https://example.com/high-resolution-image.jpg";

Once the download completes, the onload handler fires and updates the visible image instantly.


Complete Example

const image = document.getElementById("mainImage");
const downloadingImage = new Image();

downloadingImage.onload = () => {
  image.src = downloadingImage.src;
};

downloadingImage.src = "https://example.com/high-resolution-image.jpg";

That’s all you need to preload and display an image asynchronously.


Handling Errors

You can also listen for loading errors to handle missing or unreachable images gracefully.

downloadingImage.onerror = () => {
  console.error("Failed to load image.");
};

This is especially useful when loading images from external sources.


When to Use This Pattern

Asynchronous image loading is useful when:

  • You want to avoid partially rendered images
  • You’re loading large or high-resolution assets
  • You need precise control over when images appear
  • You’re building galleries, slideshows, or games

For simple pages, the browser’s native image loading is often enough. Use JavaScript loading as a progressive enhancement, not a requirement.


Final Thoughts

Loading images asynchronously with JavaScript gives you more control over performance and user experience. By preloading images in memory and displaying them only when they’re ready, you can create smoother, more polished interfaces without complex code.

As always, design your pages so they work without JavaScript first, then layer this technique on top for better performance and polish.

15 Responses to “How to Load an Image Asynchronously with JavaScript”

  1. Hi, thank you for the tutorial, but i have another related issue :
    with a button or link in the top, how can i download all the images locally using javascript ?
    Thanks a lot

  2. How would you do that to fetch multiple images asynchronously?

  3. Thank you really helpful post

  4. abraham on March 29, 2016 at 1:42 pm said:

    I use this css class to fade images in on load

    .fade-in-on-load {animation-duration: 2.3s; animation-name: fadeInOnLoad;}
    @keyframes fadeInOnLoad {
    0%,50% {opacity: 0;}
    100% {opacity: 1;}
    }

  5. Very helpful tutorial indeed. I’m looking forward to convert it for WordPress. 🙂

  6. I have some problem with the shadow domes element with the class alttext-container. It shows with a border and cant change the styles for it.

  7. A real example of how to apply it, would have been awesome.

  8. Thanks very much for this help; it was just what I needed. I learned HTML in the 90s and javascript and css are new worlds for me. This tutorial has helped me learn in general, and solve a specific use case on my site.

  9. very good on May 27, 2015 at 4:21 am said:

    good tutorial

  10. Thank you for awesome tip but I have a question. Is it effect image SEO or not?

  11. Good post, but not applicable in many cases.

  12. This is a really helpful post. Will it help me improve google page speed score?

    Have A Nice Day Buddy

  13. Really helpful post. Thank you.

Leave a Reply

You must be logged in to post a comment.

You might also like other posts...

Want to learn more about Javascript?

Learn how to use JavaScript to add interactivity to websites.

Learn more