Author Topic: Javascript Code for Rotating images?  (Read 1215 times)

I need a HTML Javascript code for rotating to a random image every time the page is refreshed/visited.
I tried Googling code but they either have advertisements, don't work, or make a slideshow.
Can anybody provide me with a link? Would be appreciated!

Personally I would use PHP for a task like this, but I'm too lazy write it out.

if all images have the same extension you could number them and then get a random number and do some crap with a span and innerhtml (although there is probably some way to mod the image tag that i still, dont quite remember)

You could have separate rotations from 1-4
Basically make 4 images

Could someone write out the script?
I don't know much about HTML, PHP, or Javascript.
D;

If you can use PHP on your webserver, do this:
1. Put all images in a folder called images
2. Create a PHP file called image.php in the parent folder of the images (eg /sdf/images, would have image.php in /sdf/)
3. Insert below code into images.php

Code: [Select]
<?php
$dir scandir("images");
if(!$dir)
{
die("Error, directory images not found");
}
$img rand(0,count($dir) - 1);
header("Location: images/" $dir[$img]);
?>


???
Profit

I cannot use PHP on the Webserver. I can however use HTML to place Javascript code.
Which is what I wanted in the first place
;-;

HTML doesn't allow images to be rotated. You won't be able to do this using HTML or JavaScript without using a hideous hack that slows down your page load and probably won't be cross-browser compatible.

Aha found it.
For those of you wondering, it's:
Code: [Select]
<script language="JavaScript">
<!--
function random_imglink(){
  var myimages=new Array()
  //specify random images below. You can have as many as you wish
  myimages[1]="img1.gif"
  myimages[2]="img2.gif"
  myimages[3]="img3.gif"

  //specify corresponding links below
  var imagelinks=new Array()
  imagelinks[1]="http://www.wsabstract.com"
  imagelinks[2]="http://www.dynamicdrive.com"
  imagelinks[3]="http://www.java-scripts.net"

  var ry=Math.floor(Math.random()*myimages.length)

  if (ry==0)
     ry=1
     document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>')
}

  random_imglink()
//-->
</script>