Question:

How can i make my web page display random images everytime they open the website

by  |  earlier

0 LIKES UnLike

i would prefer using JavaScript than php becoz php is difficult for me.Thanks for your response in advance

 Tags:

   Report

2 ANSWERS


  1. Here you go! (I love java.)

    http://simplythebest.net/scripts/DHTML_s...

    They are all free, too. : )  


  2. <html>

    <script>

    // create the list (array) of filenames

    // for the images -

    // list can grow to whatever size is needed

    var imgInfo = [

    'srcForImg0.jpg',

    'srcForImg1.jpg',

    'srcForImg2.jpg',

    'srcForImg3.jpg'

    ];

    function changeImg() {

    // how many images available?

    var last = imgInfo.length;

    // randomly select an image index

    var idx = Math.floor( last * Math.random());

    // get the source for the image

    var src = imgInfo[idx];

    var d = document;

    // get a reference to the image element

    var img = d.getElementById( 'chgImg');

    // update the image's src attribute

    img.src = src;

    }

    // every time the page is loaded, change the image

    window.onload = changeImg;

    </script>

    <body>

    <!--

    give image an id to simplify

    access via getElementById()

    -->

    <img id="chgImg" src="" />

    </body>

    </html>

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.