Random image with javascript

This little bit of javascript will give you a random image.

javascript
function randImg() {
var links= new Array(3);
links[0]='wine.jpg';
links[1]='whisky.jpg';
links[2]='beer.jpg';
var ran_unrounded=Math.random()*2;
var rand=Math.round(ran_unrounded);
var image= links[rand];
var imagehtml='<img src="'+image+'" alt="'+image.replace(/.jpg/, "")+'" />';
p = document.getElementById('rand-img');
if (p) {
 range = document.createRange();
 range.setStartBefore(p);
 p.appendChild(range.createContextualFragment(imagehtml));
 }
}

window.onload=randImg;

Here's the image...

Refresh the page and you'll see that it's pretty random.

Here's one way to do it with PHP.

Comment form

Please type the word 'Firefox' here:

BB code available :

  • [b]...[/b] : bold
  • [it]...[/it] : italic
  • [q]...[/q] : quote
  • [c]...[/c] : code
  • [url=...]...[/url] : url