Jump to content

Randomize image display - JavaScript help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
egon

egon

    Newbie

  • Members
  • PipPip
  • 13 posts
Hi there. Having trouble finding this on Google, but I would like to know how to have 2 or more images (or any object,) but only display one at a time at random. For example, if I were to go to a site with this feature, I would see "image1.jpg" but if I were to reload, I might see "image2.jpg" or "image3.jpg" at random. Thanks for any help.

#2
Blaze

Blaze

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts
So you just want a random generator to display your images? Using Javascript?

<script language="Javascript">
<!--
// please keep these lines on when you copy the source
// made by: Nicolas - http://www.javascript-page.com

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] = '0.gif'
image[1] = '1.gif'
image[2] = '2.gif'
image[3] = '3.gif'
image[4] = '4.gif'
image[5] = '5.gif'
image[6] = '6.gif'
image[7] = '7.gif'
image[8] = '8.gif'
image[9] = '9.gif'

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>