homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

reload image script
my javascript has a bug

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

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="">');

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)

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)

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);


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)

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

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved