Fog Creek Software
Discussion Board




Knowledge Base
Documentation
Terry's Tips
Darren's Tips

Inserting a random photo/image

I saw there was javascript out there for inserting random text/quotes onto a page in CityDesk. Anybody do that with images?

I would like to rotate a "welcome" image on my index page that would change each time someone visited or refreshed the page. Anybody have some help with this? I assume you set up a folder with the images in it, but then my lack of knowledge in javascript escapes me...

Brian
Friday, July 09, 2004

The JavaScript would look like ...

<script language="JavaScript">
<!--
var imagelist = new Array()
imagelist[0]="image1.jpg"
imagelist[1]="image2.jpg"
imagelist[2]="image3.jpg"
imagelist[3]="image4.jpg"

var SomeNumber=Math.floor(Math.random()*imagelist.length);
document.write ( "<img src=\"" + imagelist[SomeNumber] + "\">");
//-->
</script>

Dan
Friday, July 09, 2004

So what you need to do is put a bit of CityScript where the imagelist[x] = "filename" bit.

I'm just wondering about CityScript incrementing the x though

Dan
Friday, July 09, 2004

Revised script (JavaScript only):

<script language="JavaScript">
<!--
document.writeln("Hello<br />");

var imagelist = new Array("image1.jpg","image2.jpg","image3.jpg","image4.jpg");

var SomeNumber=Math.floor(Math.random()*imagelist.length);
document.write ( "<img src=\"" + imagelist[SomeNumber] + "\">");
//-->
</script>

So now all your filenames are in a list which is much easier to script with CD.

Only ... I've just remembered ... CityDesk won't include image file names (racking my brain, this is correct isn't it). If you were looping through article filenames no problem, but for images the imagelist array would be hardcoded (can someone verify please. It's Friday afternoon to me:-)

Dan
Friday, July 09, 2004

Something like this might work but it might be easier to  put the image file names into the Javascript.

If you make articles named "image1" "image2," etc., that is, make file names that are the same as your image names, you can do a script  like this:

{$for each x in (folder "randomphoto")$}
{$.filename$}.JPG,
{$next$}

tk
Friday, July 09, 2004

Or you could use this idea: http://automaticlabs.com/products/rotator

Jorgen B.
Friday, July 09, 2004

*  Recent Topics

*  Fog Creek Home