Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Old 04-15-2007, 04:14 PM random image problem
Junior Talker

Posts: 1
Trades: 0
hello,

i use a simple code below to choose a random image for my site header:

<script language="javascript" type="text/javascript">

p=65*Math.random();
d=parseInt(p);
document.write ("<img src ='L:/webroot/pics/"+d+".jpg'>");

</script>

there are 65 images in the folder, starting from 0.

all is working pretty wel, but - sometimes it gives a blank result, not the image.

can You please help?
thx
blobb is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-15-2007, 05:11 PM Re: random image problem
Republikin's Avatar
Defies a Status

Posts: 3,189
Trades: 3
Perhaps try using the IsNaN function to do some error checking to make absolutely sure your getting a correct integer and not just NaN.

edit:
Just another little flaw in your logic I just noticed is that by multiplying by 65 your never going to show picture number 65 since even 65*0.99 will only result in 64.35. Thus since parseInt only removes everything after the decimal (including the decimal) your highest possible result will be 64. So, try multiplying by 66 instead.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Last edited by Republikin; 04-15-2007 at 05:15 PM..
Republikin is offline
Reply With Quote
View Public Profile
 
Old 04-16-2007, 12:03 AM Re: random image problem
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,938
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
That's not a flaw. If he starts at 0, the last file number will be 64, not 65.

I'm thinking that the reason your code is acting up is because it's accessing a file that does not exist. You should make sure that you have all images from 0 to 64 in your folder.

If not, use alert (d) to see that you're getting the correct numbers.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 04-16-2007, 11:23 AM Re: random image problem
Republikin's Avatar
Defies a Status

Posts: 3,189
Trades: 3
Quote:
Originally Posted by ADAM Web Design View Post
That's not a flaw. If he starts at 0, the last file number will be 64, not 65.
Good catch.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Republikin is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to random image problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.19334 seconds with 12 queries