Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    kjs
    kjs is offline
    New to the CF scene
    Join Date
    May 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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. ;)

  • #3
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,522
    Thanks
    3
    Thanked 508 Times in 495 Posts
    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/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #4
    kjs
    kjs is offline
    New to the CF scene
    Join Date
    May 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #5
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by boxxer03; 05-25-2006 at 12:20 AM.
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #6
    kjs
    kjs is offline
    New to the CF scene
    Join Date
    May 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's working fine as it is chaps.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •