Stick all the images in a single folder with file names from 001-200. Then just have a script that picks a random number from between those values on page load and updates the image src. Answer from micppp on reddit.com
🌐
Reddit
reddit.com › r/html5 › choosing a random image to display from one url image link?
r/html5 on Reddit: Choosing a random image to display from one URL image link?
June 6, 2023 -

Is there a tool that lets me use one URL (mysite.com/random-image for example) that dynamically updates the image in that URL to a random image of let's say 200 images at specific intervals? I'd like to display a random image on my site on every page load, or every minute, but don't want to code in 200+ image URLs in the HTML. Thanks for the help! Edit: If JavaScript is needed that's fine

🌐
Randomimageurl
randomimageurl.com
Random Image URL - Find Random Image URLs
Whether you’re a web developer, designer, or content creator, this free tool helps you quickly generate random image links for testing, placeholder content, or creative projects. No need to upload or search manually — just click Generate to instantly get a unique image link every time. Copy the URL and use it in your HTML, CSS, or JavaScript projects, demo websites, or app prototypes.
Discussions

Generating a random image with a link
Samuel Glister is having issues with: Hi All, I'm at work and I need to try and generate a random image with a link behind it. This code will then need need to post itself in... More on teamtreehouse.com
🌐 teamtreehouse.com
2
April 25, 2016
Random images on 'refresh' in HTML without JavaScript - Stack Overflow
I once saw in a tutorial video about an HTML line of code that generates random images that it fetches from the internet and puts into the webpage, I remember it being a normal img tag but inside the ref attribute contained a link that now I don't remember what it was. More on stackoverflow.com
🌐 stackoverflow.com
Display random image with fitting link - javascript
I'm using the below code to show random images which works fine. How can I add to this code so that each image gets it's own link? I.e. if "knife.png" is randomly picked I want it to have a link that takes the user to "products/knife.html" and so on. Thanks for any help! More on stackoverflow.com
🌐 stackoverflow.com
Choosing random image to display from one URL image link?
You're basically describing an API endpoint, depending on what sort of frontend you're using, there's several ways to do it. HTTP request to that endpoint which does whatever logic to return a url. Pretty straightforward. More on reddit.com
🌐 r/webdev
5
4
June 6, 2023
🌐
Lorem Picsum
picsum.photos
Lorem Picsum
Just add your desired image size (width & height) after our URL, and you'll get a random image.
🌐
Media College
mediacollege.com › internet › javascript › image › random-link.html
How to Display a Random Image With Hyperlink
Home : Internet : JavaScript : Random Image with Link · This Javascript is a variation on the random image code that adds the option to make each image a hyperlink. This is a simplified version—note that each image's full path must be included in the image attributes. <script type="text/javascript"> var total_images = 3; var random_number = Math.floor((Math.random()*total_images)); var random_img = new Array(); random_img[0] = '<a href="page1.html"><img src="images/image1.gif"></a>'; random_img[1] = '<a href="page2.html"><img src="images/image2.gif"></a>'; random_img[2] = '<a href="page3.html"><img src="images/image3.gif"></a>'; document.write(random_img[random_number]); </script> Note: You can have any number of random images—just make sure you change the "total_images" variable to reflect the total number.
🌐
Team Treehouse
teamtreehouse.com › community › generating-a-random-image-with-a-link
Generating a random image with a link (Example) | Treehouse Community
April 25, 2016 - var total_images = 3; var image ... = []; random_img[0] = '<a href="https://google.com"><img src="http://placehold.it/350x150"></a>'; random_img[1] = '<a href="https://google.com"><img src="http://placehold.it/350x250"></a>'; random_img[2] = '<a href="https://google.com"><img src="http://placehold.it/350x350"></a>'; image.innerHTML = random_img[random_number]; ... That is ...
🌐
Computer Hope
computerhope.com › javascript › random-image.htm
JavaScript Random Image and Link
<script type="text/javascript"> //Javascript Created by Computerhope https://www.computerhope.com/ //store the quotations in arrays var images = [], index = 0; images[0] = "<a href = 'https://www.computerhope.com/'><img src='https://www.computerhope.com/banners/banner.gif' alt='Visit Computer Hope'></a>"; images[1] = "<a href = 'https://www.computerhope.com/history'><img src='https://www.computerhope.com/banners/banner2.gif' alt='Computer History'></a>"; images[2] = "<a href = 'https://www.computerhope.com/'><img src='https://www.computerhope.com/banners/banner3.gif' alt='Visit Computer Hope'></a>"; index = Math.floor(Math.random() * images.length); document.write(images[index]); //done </script> See our JavaScript and random terms for further information.
🌐
Isotropic
isotropic.co › how to generate random images by url (web dev)
How To Generate Random Images By URL (Web Dev) - Isotropic
June 7, 2021 - The API makes it easy to find random images, in the form of a URL, which makes it really easy to add to any webpage using the <picture> or <img> element in HTML.
Find elsewhere
🌐
CodePen
codepen.io › k2loo › pen › zbMLbN
Unsplash random images
In CodePen, whatever you write in the HTML editor is what goes within the <body> tags in a basic HTML5 template. If you need things in the <head> of the document, put that code here. ... The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
🌐
phpBB
phpbb.com › board index › phpbb archives › phpbb 3.0.x forums archive › [3.0.x] support forum
phpBB • Random image html page
Location: Igloos for sale... bearly used ... . UPDATE! I put about 20 images into the array and ugh... slowed the load time on the board right down in IE ... Firefox was ok. I was worried about it with IE so it wasn't a surprise, really. The average image weight was about 30kb. So, I took a blank html page and placed the random image javascript in the header and in the body.
🌐
Medium
medium.com › @yuvaraj.io › get-random-images-for-free-cfdeb0490ca5
Get Random Images for free - Yuvaraj S - Medium
February 25, 2023 - While developing a small image gallery using HTML, CSS, JS. I encountered this beautiful website. ... You can give your desired Dimension of the photo at the last part of the path. Ie, 200px as the height and 300px as the width. In our Example. Successfully I was able to add some images to my, Here every time I click refresh browser I will get a new and fresh image. ... Frontend Developer with 7years of experience, I write blogs that are [Easy words + Ground Level Code + Live Working Link]. for mor info: https://yuvaraj.io
Top answer
1 of 4
2

Generalize your list of images so that that it can be multi-purposed - you can add additional information later. Then surround the image by an anchor tag (<a>) and use the following.

<div id="box">
  <a name="imagelink"><img id="image" /></a>
</div>

<script type='text/javascript'> 
  var images = ["knife","fork","spoon","chopsticks",];

  function randImg() {
    var size = images.length
    var x = Math.floor(size * Math.random())
    document.getElementById('image').src = "images/"+images[x]+".png";
    document.getElementById('imagelink').href="products/"+images[x]+".html";
  }
</script>

randImg();
2 of 4
1

Try doing something like this example. Like all these other guys have said, it's better to store this info in a database or something, but to answer your question, put the values you need into an object in the array and reference the properties of the object instead of just having a string array.

<div id="box">
    <a id="a"><img id="image" /></a>
</div>

<script type='text/javascript'> 
var images = 
[
    imageUrlPair = { ImgSrc:"http://www.dansdata.com/images/bigknife/bigknife1280.jpg", Href:"http://reluctantgourmet.com/tools/cutlery/item/267-chefs-knife-choosing-the-right-cutlery" },
    imageUrlPair = { ImgSrc:"http://www.hometownsevier.com/wp-content/uploads/2011/01/fork.jpg", Href:"http://eofdreams.com/fork.html" },
    imageUrlPair = { ImgSrc:"http://upload.wikimedia.org/wikipedia/commons/9/92/Soup_Spoon.jpg", Href:"http://commons.wikimedia.org/wiki/File:Soup_Spoon.jpg" },
    imageUrlPair = { ImgSrc:"http://upload.wikimedia.org/wikipedia/commons/6/61/Ouashi.jpg", Href:"http://commons.wikimedia.org/wiki/Chopsticks" },
]

function randImg() {
var size = images.length;
var x = Math.floor(size * Math.random());
var randomItem = images[x];
document.getElementById('image').src = randomItem.ImgSrc;
document.getElementById('a').href = randomItem.Href;
}

randImg();
</script>
Top answer
1 of 9
16

Wombleton's answer is what I would do. However, there is another way to do it. In the body markup, wherever you are going to put that random image, put a script that does a document.write with the markup for the image. Make sure you have an array of image URLs and substitute, like so:

<html>
<head>
<title>Test</title>
<script type="text/javascript">
  var imageURLs = [
       "http://www.myserver.com/images/image1.png"
     , "http://www.myserver.com/images/image2.png"
     , "http://www.myserver.com/images/image3.png"
  ];
  function getImageTag() {
    var img = '<img src=\"';
    var randomIndex = Math.floor(Math.random() * imageURLs.length);
    img += imageURLs[randomIndex];
    img += '\" alt=\"Some alt text\"/>';
    return img;
  }
</script>
</head>
<body>
<script type="text/javascript">
  document.write(getImageTag());
</script>
</body>
</html>

Again, this is not ideal, but is useful if you don't want to use any kind of onload event, not just the one you put in the <body> tag.

2 of 9
4

Adapted from jQuery's ready function, and making some assumptions about the image types:

(function() {
  var urls = ['1', '2', '3', '4'];
  function swap() {
    document.getElementById('theImage').setAttribute('src', urls[Math.round(Math.random() * urls.length)] + '.jpg');
  }

  // Mozilla, Opera and webkit nightlies currently support this event
  if ( document.addEventListener ) {
    window.addEventListener( 'load', swap, false );
  // If IE event model is used
  } else if ( document.attachEvent ) {
    window.attachEvent( 'onload', swap );
  }
})();
🌐
Code with Mosh
forum.codewithmosh.com › html/css
HTML Layout Exersize - Unsplash Random Image Not Working - HTML/CSS - Code with Mosh Forum
The random image URL given in this exersize is not working 13- Exercises | Code with Mosh Here is the URL “You can set your image source to https://source.unsplash.com/collection/190727/800x600 to get a random image.”
Published   August 13, 2024
🌐
CodePen
codepen.io › jsd041313 › pen › RQEOWm
Random Picture Generator
Format HTML · View Compiled HTML · Analyze HTML · Maximize HTML Editor · Minimize HTML Editor · Fold All · Unfold All · <html> <head> <title>Random Picture Generator</title> <link rel="stylesheet" href="assets/stylesheets/app.css"> </head> ...
🌐
Reddit
reddit.com › r/webdev › random image in img tag?
r/webdev on Reddit: Random image in IMG tag?
July 6, 2024 -

I'm trying to create a simple page, on a site that allows limited HTML but no scripting or other advanced stuff. To spice things up, I thought it would be fun to have pictures which are randomised when you reload the page.

A while ago, I discovered Unsplash Source, an API for linking to pictures from the stock photo site Unsplash. With this nifty tool, I could make a collection of pictures on their site, and link to a random picture from the collection. Just <img src=https://source.unsplash.com/collection/9357731/x1>, and I'd have a random picture!

Sadly, the whole thing has apparently been shut down, so now I need something else that allows me to sneak a randomiser into an img-tag. Ideally somewhere I can upload or link to my own pictures, rather than just picking from that one stock photo site.

I do have a little home server, so it's presumably possible to set up my own randomiser. But I'm a beginner at web development, and haven't managed to figure out SSL, so my self-hosted pictures are rejected by the site in question for not using HTTPS. Much frustration!

Are there image hosting sites that use a similar clever linking interface? Can I somehow funnel my self-hosted pictures through someone else's HTTPS? Any other clever tricks for accomplishing this?

🌐
Coding Artist
codingartistweb.com › home › javascript › how to build a random images generator with html, css, and javascript
How to Build a Random Images Generator with HTML, CSS, and JavaScript | Coding Artist
January 22, 2025 - This simple project will display random images every time a button is clicked, making use of the Picsum API to fetch beautiful placeholder images. ... Understand how to structure a web page with HTML.
🌐
Vercel
random-image-pepebigotes.vercel.app
random-image by PepeBigotes
MARKDOWN: ![Random image](https://random-image-pepebigotes.vercel.app/api/random-image) HTML: &ltimg alt="Random image" src="https://random-image-pepebigotes.vercel.app/api/random-image"&gt