Welcome to WebmasterWorld Guest from 54.145.136.73

Forum Moderators: open

reload image script

my javascript has a bug

   
3:11 pm on Apr 4, 2003 (gmt 0)

10+ Year Member



hi
what is wrong with this:
<SCRIPT LANGUAGE="javascript">
var img_rnd = new Array ("image1.jpg", "image2.jpg", "image3.jpg");
var i = Math.round(Math.random() * 3);
document.write('<IMG SRC="images' + img_rnd[i] + '" WIDTH="300" HEIGHT="207" BORDER="0" ALT="">');
</SCRIPT>

it seems to function properly for a while, but suddenly theres no image to show when i reload.. when i reload again, it works..
my page is: <snip>

[edited by: korkus2000 at 6:10 pm (utc) on April 4, 2003]
[edit reason] No personal URLs please [/edit]

6:12 pm on Apr 4, 2003 (gmt 0)

WebmasterWorld Senior Member mivox is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Welcome to WebmasterWorld, golovan... Hopefully you'll find a javascript whiz here in the Browsers/HTML forum who can work things out, because that doesn't look like any javascript I've used on any of my sites. :)
6:18 pm on Apr 4, 2003 (gmt 0)

WebmasterWorld Senior Member korkus2000 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Welcome to the board golovan,

You have 3 items in your array. Javascript starts at 0 with arrays. You are generating numbers from 0 - 3. You only need numbers from 0 - 2.

change this

var i = Math.round(Math.random() * 3);

to

var i = Math.round(Math.random() * 2);

or add a fourth picture.

<edit>fixed typo</edit>

[edited by: korkus2000 at 6:47 pm (utc) on April 4, 2003]

6:38 pm on Apr 4, 2003 (gmt 0)

10+ Year Member



hehe..
i knew it wasn't all to hard... but sometimes it's impossible to see the easy way out..
thanks guys!
 

Featured Threads

Hot Threads This Week

Hot Threads This Month