Placeholder Images for AJAX
A placeholder image stands in for the real picture while a page is being built, so the layout can be tested before the final images exist. Lorem Picsum (picsum.photos) is a free service that returns real photos at whatever size the URL asks for, and it also has a JSON API that lists its images, which makes it useful for practising AJAX.
Images by URL
Section titled “Images by URL”The width and height go in the path. Every URL below returns a JPEG, so each one can be used directly as the src of an <img> element.
| URL | Returns |
|---|---|
https://picsum.photos/400/300 |
a random photo, 400 by 300 pixels |
https://picsum.photos/300 |
a random square photo |
https://picsum.photos/id/237/400/300 |
photo number 237 at that size |
https://picsum.photos/seed/teapot/400/300 |
the same photo every time for the word teapot |
https://picsum.photos/400/300?grayscale |
a black-and-white photo |
https://picsum.photos/400/300?blur=2 |
a blurred photo, with blur from 1 to 10 |
https://picsum.photos/400/300.webp |
a WebP file instead of a JPEG |
The random URLs give a different photo on each request, but the browser caches a URL it has already loaded. Several identical random URLs on one page can therefore show the same photo, and adding a different query value to each, such as ?random=1 and ?random=2, keeps them apart. A seed is the better choice where the photo should stay the same between visits.
Listing the images with fetch()
Section titled “Listing the images with fetch()”The endpoint https://picsum.photos/v2/list returns an array of image records, a page at a time. page picks the page and limit sets how many records it holds.
const response = await fetch('https://picsum.photos/v2/list?page=3&limit=4');const photos = await response.json();
for (const photo of photos) { console.log(photo.id, photo.author, `${photo.width}x${photo.height}`);}console.log(photos[0].download_url);This prints:
8 Alejandro Escamilla 5000x33339 Alejandro Escamilla 5000x326910 Paul Jarvis 2500x166711 Paul Jarvis 2500x1667https://picsum.photos/id/8/5000/3333Each record also has a url field, which links to the photo’s page on Unsplash rather than to an image file. download_url points at the full-size file, which for most photos is several megabytes, so it is better to build a smaller URL from the id. The details of a single image are at https://picsum.photos/id/<id>/info.
A gallery on a page
Section titled “A gallery on a page”This snippet loads one page of the list and adds a small thumbnail for each photo, with the photographer’s name as its alternative text.
<button id="load-gallery">Load photos</button><div id="gallery"></div>const button = document.getElementById('load-gallery');const gallery = document.getElementById('gallery');
button.addEventListener('click', async () => { try { const response = await fetch('https://picsum.photos/v2/list?page=2&limit=6'); if (!response.ok) { throw new Error(`HTTP ${response.status}`); } const photos = await response.json();
const images = photos.map((photo) => { const img = document.createElement('img'); img.src = `https://picsum.photos/id/${photo.id}/200/150`; img.alt = `Photo by ${photo.author}`; img.width = 200; img.height = 150; return img; }); gallery.replaceChildren(...images); } catch (err) { gallery.textContent = `Could not load the photos: ${err.message}`; }});Clicking the button fills the div with six thumbnails, each 200 by 150 pixels. replaceChildren() removes whatever the div held before, so clicking again does not add a second set. Setting width and height reserves the space for each image before it loads, which stops the page from jumping as the photos arrive.
Steve Griffith covers this topic in his video Placeholder Images for AJAX.