Welcome to WebmasterWorld Guest from 50.19.0.90

Forum Moderators: open

Message Too Old, No Replies

reload image script

my javascript has a bug

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

New User

10+ Year Member

joined:Mar 14, 2003
posts:4
votes: 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]

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

Senior Member

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

joined:Dec 6, 2000
posts:3928
votes: 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. :)
6:18 pm on Apr 4, 2003 (gmt 0)

Senior Member

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

joined:Mar 20, 2002
posts:3732
votes: 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]

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

New User

10+ Year Member

joined:Mar 14, 2003
posts:4
votes: 0


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

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members