display a random image on reload


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: display a random image on reload

  1. #1
    charlie Guest

    display a random image on reload


    I want the visitor to view a different image on my home page when they reload.
    How can I get 3 random images to change on page reload. I had this script
    but it doesn't work.

    <script language="JavaScript">
    var random=Math.round(Math.random())*1
    var numberA=(random)+1
    var numberB=(numberA)+(random)
    if (numberA<=2)
    document.write('<img src="2.gif">')
    else if (numberB<=1)
    document.write('<img src="1.gif">')
    else if (numberB<=3)
    document.write('<img src="5.gif">')
    </script>

    HELP!
    Email me if you can. Thanx!

  2. #2
    Christian Yungk Guest

    Re: display a random image on reload


    Try this...

    <SCRIPT language="JavaScript">
    var random = Math.round((Math.random() * 3) + .5);
    var image;
    switch(random)
    {
    case 1: image="1"; break;
    case 2: image="2"; break;
    case 3: image="5"; break;
    }
    document.write("<img src=\"" + image + ".gif\"">");
    </SCRIPT>


    "charlie" <charliedhq@hotmail.com> wrote:
    >
    >I want the visitor to view a different image on my home page when they reload.
    >How can I get 3 random images to change on page reload. I had this script
    >but it doesn't work.
    >
    ><script language="JavaScript">
    >var random=Math.round(Math.random())*1
    >var numberA=(random)+1
    >var numberB=(numberA)+(random)
    >if (numberA<=2)
    >document.write('<img src="2.gif">')
    >else if (numberB<=1)
    >document.write('<img src="1.gif">')
    >else if (numberB<=3)
    >document.write('<img src="5.gif">')
    ></script>
    >
    >HELP!
    >Email me if you can. Thanx!



  3. #3
    marc Guest

    Re: display a random image on reload


    Math.random generates a random number between 0 and 1. Multiplying the result
    by one and then rounding it off means that you can only get 0 or 1 as a result.
    This is what I suggest you do:

    <script language="JavaScript">
    document.write('<img src="' + (Math.round(Math.random()*2) + 1) + '.gif');
    </script>

    This will write one of:
    <img src="1.gif"> or
    <img src="2.gif"> or
    <img src="3.gif">
    to the document.

    If you name your images differently (i.e. not 1.gif, etc.) you may want to
    use a case statement to select the image.

    Marc

    "charlie" <charliedhq@hotmail.com> wrote:
    >
    >I want the visitor to view a different image on my home page when they reload.
    >How can I get 3 random images to change on page reload. I had this script
    >but it doesn't work.
    >
    ><script language="JavaScript">
    >var random=Math.round(Math.random())*1
    >var numberA=(random)+1
    >var numberB=(numberA)+(random)
    >if (numberA<=2)
    >document.write('<img src="2.gif">')
    >else if (numberB<=1)
    >document.write('<img src="1.gif">')
    >else if (numberB<=3)
    >document.write('<img src="5.gif">')
    ></script>
    >
    >HELP!
    >Email me if you can. Thanx!



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center