| How do I display a randomly selected image? |
| User Opinions |
|
No users have voted.
|
|
Thank you for rating this answer.
|
To randomly insert one of a group of images into a web page, you can use this simple Javascript code. Just insert this code where you would otherwise place an ordinary < img > element. Note the comments: you must set images to the correct number of images, and you must name your images image1.jpg, image2.jpg, etc. in order to use this script with them.
A different image will appear at the appropriate point in the page every time the page is loaded or reloaded.
If the user has disabled Javascript, the image backupimage.jpg appears instead. <script>
<!--
// CHANGE THIS to the number of images that exist.
// YOU MUST NAME YOUR IMAGE FILES image1.jpg, image2.jpg,
// image3.jpg, etc. up to and INCLUDING this number.
images = 7;
number = Math.floor(Math.random(1) * images) + 1;
document.write("<img src=\"image" + number + ".jpg\">");
// -->
</script>
<noscript>
<img src="backupimage.jpg">
</noscript>
|
| Visitor Comments |
|
No visitor comments posted. Post a comment
|
| Attachments |
|
No attachments were found.
|