Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-24-2006, 01:01 AM   PM User | #1
kjs
New to the CF scene

 
Join Date: May 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
kjs is an unknown quantity at this point
Making "Random Image" code more *Random*

Hello all,
Ok so I've got this code to randomise the appearance of images on my site, but as the case stands, it doesn't actually do as I expected. Say I click refresh, all it does is go to the next image in the order in which I have listed them. Anyway, here's the code, and I was just wondering if any of you guys could help me to make it more spontaneous, and not just got from image 1 to image 2, then 3, then image 4 etc.

Thanks alot in advance everyone, here's the code:-

Code:
<script language="Javascript">

var currentdate = 0
var core = 0

function StringArray (n) {
  this.length = n;
  for (var i =1; i <= n; i++) {
    this[i] = ' '

  }
}

image = new StringArray(10)
image[0] = 'Images/CelebrityFish/anthonystewarthammerheadhea.jpg'
image[1] = 'Images/CelebrityFish/ashleeshrimpsonsimpson.jpg'
image[2] = 'Images/CelebrityFish/bananapirahnarama.jpg'
image[3] = 'Images/CelebrityFish/bennygillhill.jpg'
image[4] = 'Images/CelebrityFish/billgillmurray.jpg'
image[5] = 'Images/CelebrityFish/carlcarplewis.jpg'
image[6] = 'Images/CelebrityFish/cliffpilchardrichard.jpg'
image[7] = 'Images/CelebrityFish/davidgraylinggray.jpg'
image[8] = 'Images/CelebrityFish/dolphdolphinlundgren.jpg'
image[9] = 'Images/CelebrityFish/ericclamtunacantona.jpg'

var ran = 60/image.length

function ranimage() {
  currentdate = new Date()
  core = currentdate.getSeconds()
  core = Math.floor(core/ran)
    return(image[core])
}

document.write("<img src='" +ranimage()+ "'>")

</script>
Cheers, KJS.
kjs is offline   Reply With Quote
Old 05-24-2006, 06:10 AM   PM User | #2
boxxer03
Regular Coder

 
Join Date: Sep 2005
Location: Chicago, IL
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
boxxer03 is an unknown quantity at this point
Try this:

Code:
<script type="text/javascript">
function randomimage(){
image = new Array()
image[0] = 'Images/CelebrityFish/anthonystewarthammerheadhea.jpg'
image[1] = 'Images/CelebrityFish/ashleeshrimpsonsimpson.jpg'
image[2] = 'Images/CelebrityFish/bananapirahnarama.jpg'
image[3] = 'Images/CelebrityFish/bennygillhill.jpg'
image[4] = 'Images/CelebrityFish/billgillmurray.jpg'
image[5] = 'Images/CelebrityFish/carlcarplewis.jpg'
image[6] = 'Images/CelebrityFish/cliffpilchardrichard.jpg'
image[7] = 'Images/CelebrityFish/davidgraylinggray.jpg'
image[8] = 'Images/CelebrityFish/dolphdolphinlundgren.jpg'
image[9] = 'Images/CelebrityFish/ericclamtunacantona.jpg'

var pickimage = Math.floor(Math.random()*image.length);
document.write('<img src="' +image[pickimage]+ '">');
}
randomimage();
</script>
__________________
I'm only telling you how I would do it, not how its supposed to be done. ;)
boxxer03 is offline   Reply With Quote
Old 05-24-2006, 07:45 AM   PM User | #3
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,358
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
a cookie will be necessary to ensure that the last image is not selected again
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Old 05-24-2006, 09:47 PM   PM User | #4
kjs
New to the CF scene

 
Join Date: May 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
kjs is an unknown quantity at this point
Legends. Thanks guys.

Boxxer, it worked a treat, and Phillips, thank you for the cautionary advice.

This place is a breath of fresh air - no idiots evident, and genuine, prompt and helpful advice.

Thanks alot!
kjs is offline   Reply With Quote
Old 05-25-2006, 12:18 AM   PM User | #5
boxxer03
Regular Coder

 
Join Date: Sep 2005
Location: Chicago, IL
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
boxxer03 is an unknown quantity at this point
You don't necessarily need a cookie, you could always do add in a line or two that keeps track of what image it is currently on and when it goes to switch images have it check and make sure they're not the same picture.

edited: my bad, he's right you would need to use a cookie. for some reason i was thinking you were using a timer and rotating them that way. my bad.
__________________
I'm only telling you how I would do it, not how its supposed to be done. ;)

Last edited by boxxer03; 05-25-2006 at 12:20 AM..
boxxer03 is offline   Reply With Quote
Old 05-25-2006, 12:37 AM   PM User | #6
kjs
New to the CF scene

 
Join Date: May 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
kjs is an unknown quantity at this point
It's working fine as it is chaps.
kjs is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 08:32 PM.


Advertisement
Log in to turn off these ads.