random image

Discussion in 'JavaScript' started by rojojat, Dec 15, 2008.

  1. #1
    Hi,
    I am looking for a javascript code that will display a different image each time the web page is displayed, or refreshed.
    Thanks.
     
    rojojat, Dec 15, 2008 IP
  2. astupidname

    astupidname Peon

    Messages:
    12
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Dumb Title</title>
    <script type="text/javascript">
    /*<![CDATA[*/
    
    function myRandomImg(id) {
        var pics = ["images/1m.gif", "images/2m.gif", "images/3m.gif", "images/4m.gif", "images/5m.gif"];
        var rNum = Math.floor(Math.random() * pics.length);
        document.getElementById(id).src = pics[rNum];
    } 
    
    window.onload = function () {
        myRandomImg('randImg');
    };
    
    /*]]>*/
    </script>
    </head>
    <body>
    <div>
    <img src="images/1m.gif" alt="1m.gif" id="randImg" />
    </div>
    </body>
    </html>
    HTML:
     
    astupidname, Dec 16, 2008 IP
  3. rojojat

    rojojat Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank You.
     
    rojojat, Dec 17, 2008 IP