Acmlm's Board - I2 Archive - Programming - Random images in HTML
User | Post |
Modereb
Posts: 46/75 |
If you put this in a HTML file:
<script type="text/javascript"> document.write('<img src="random_image'+Math.floor(Math.random()*3)+'.jpg" alt="image" />'); </script>
Either <img src="random_image0.jpg" alt="image" />, <img src="random_image1.jpg" alt="image" /> or <img src="random_image2.jpg" alt="image" /> is outputted by the Javascript.
So, basically, just paste that code anywhere where you want to have the random image in your HTML file, and make sure the images random_image0.jpg, random_image1.jpg and random_image2.jpg are there. |
alitnil
Posts: 122/290 |
Modereb, I'll go with the Javascript, but surprisingly enough, I am confused (shocker).
document.write('');
the image src= section, that is where i would type out the file location correct? as in http://www.website.com/file?
|
Zem
Posts: 142/1107 |
For PHP, it's more common to link to an image.php, so you can do it on servers you don't control.
<?php header("Content-type: image/jpg"); // this may not be necessary. I don't remember. It wouldn't hurt. header("Cache-Control: no-store, no-cache, must-revalidate"); // prevent caching so it's different every reload
readfile("image" . rand(0, 2) . ".jpg"); ?> Of course, you probably don't have access to a php server. In which case you'd have to do the Javascript one, which this board wouldn't allow you to do. |
Modereb
Posts: 42/75 |
You can't with just HTML, you either have to use Javascript, or a server side language such as ASP or PHP.
Examples that will output image0.jpg, image1.jpg or image2.jpg...
Javascript: document.write('<img src="image'+Math.floor(Math.random()*3)+'.jpg" alt="image" />');
PHP: <? echo "<img src=\"image".rand(0,2).".jpg\" alt=\"image\" />"; ?>
ASP <% randomize randnumber=int(rnd*2)+0 response.write randnumber %> |
alitnil
Posts: 120/290 |
how would I code for an image that is randomized each time you visit or refresh the page in HTML terms? |
|