Back to main page!

Knowledgebase Home Knowledgebase Home
Search the Knowledgebase Browse by Category
How do I display a randomly selected image?
User Opinions
No users have voted.

How would you rate this answer?
Helpful
Not helpful
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
Related Questions
Attachments
No attachments were found.

Home | Web Design | Print Advertisements | Banner Design | Other Services


Copyright © 2003 Digital Arts Group @ FrankGonzalez.com. All Rights Reserved
About Us | Disclaimer | Privacy Policy | Terms of Use | Design Team | Sitemap