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.
Contents
Overview
The basic process for loading an image asynchronously looks like this:
- Create a new
Imageobject in JavaScript - Assign a URL to its
srcattribute to start downloading - Listen for the
loadevent - 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.

Cool dawh
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
You need to create an array of images and load your source in a loop.
How would you do that to fetch multiple images asynchronously?
Thank you really helpful post
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;}
}
Very helpful tutorial indeed. I’m looking forward to convert it for WordPress. 🙂
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.
A real example of how to apply it, would have been awesome.
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.
good tutorial
Thank you for awesome tip but I have a question. Is it effect image SEO or not?
Good post, but not applicable in many cases.
This is a really helpful post. Will it help me improve google page speed score?
Have A Nice Day Buddy
Really helpful post. Thank you.