| reload image script my javascript has a bug |
golovan

msg:1483883 | 3:11 pm on Apr 4, 2003 (gmt 0) | 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]
|
mivox

msg:1483884 | 6:12 pm on Apr 4, 2003 (gmt 0) | 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. :)
|
korkus2000

msg:1483885 | 6:18 pm on Apr 4, 2003 (gmt 0) | 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]
|
golovan

msg:1483886 | 6:38 pm on Apr 4, 2003 (gmt 0) | hehe.. i knew it wasn't all to hard... but sometimes it's impossible to see the easy way out.. thanks guys!
|
|
|