Click here to Skip to main content
15,442,754 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I'm trying to fetch images from another URL using JavaScript or jQuery. The image URL is sequential.

For example: etc...

There are over 5000+ images so just inserting as a IMG, SRC attribute won't be possible.

<img src="1.jpg"></img> <br>
<img src="2.jpg"></img> <br>
<img src="3.jpg"></img> <br>
<img src="4.jpg"></img> <br>
<img src="5.jpg"></img> <br>
<img src="6.jpg"></img> <br>
<img src="7.jpg"></img> <br>
<img src="8.jpg"></img> <br>
<img src="9.jpg"></img> <br>
<img src="10.jpg"></img> <br>

My goal is to create a some kind of loop function to fetch all the series of images and display them on the page.

Thank you for reading.

What I have tried:

I've done research after all no luck. If it's possible to create some type of function that performs above I would really appreciate that.
Updated 1-Feb-22 9:19am
Dave Kreskowiak 1-Feb-22 12:37pm     CRLF
5000+ images on a single page? I don't think you're going to get away with it because of resource constraints. How much memory are all those images going to take up? Good luck with that.
PuppyDude 1-Feb-22 12:51pm    
Hello thanks for the reply! yeah that seems little bit crazy. Maybe adding lazy loading will help? any ideas about that?
Dave Kreskowiak 1-Feb-22 13:13pm     CRLF
You're thinking of it from the client side. From the server side, you've got a single page load that's making over 5,000+ requests. That's ... abuse of the server, to say the least. Making the images "lazy" loaded just spreads that many request out over a longer period of time. Now do it for 10 clients and you've got 50,000+ requests to service.
Richard Deeming 2-Feb-22 3:42am    
Browsers will limit the number of concurrent requests made to the same host, usually to around six. The page will take longer to load, but the server shouldn't be flooded with requests for a single client.
Dave Kreskowiak 2-Feb-22 9:36am     CRLF
Yeah, the point is this is a really bad idea. Depending on the size of the images files, this can be quite a large download, and when those images get reconstituted for display on the client side, they can expand to take GBs of memory. Depending on the use of the images, it might be a much better idea to convert the images to a video and just stream that.

1 solution

<div id="output"></div>
const output = document.getElementById("output");
for (let i = 1; i < 5000; i++) {
    const img = document.createElement("img");
    img.src = `${i}.png`;
    const br = document.createElement("br");
Document.createElement() - Web APIs | MDN[^]
Node.appendChild() - Web APIs | MDN[^]

Be prepared for the page to take an extremely long time to load, and potentially crash your browser when it runs out of memory. Consider marking the images for lazy loading:
Lazy loading - Web Performance | MDN[^]
Share this answer
PuppyDude 1-Feb-22 12:44pm    
What a nice community!!! Thank you sir!
Dave Kreskowiak 1-Feb-22 12:56pm     CRLF
Whoops. Replied to the wrong post.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900