Old 09-04-2003, 11:20   #1 (permalink)
dixon
Custom User Title
 
dixon's Avatar
 
Join Date: Apr 2003
Location: Brighton
Posts: 628
random picture!

hey guys i was wondering if someone could tell me how to make pictures shown at random.
for example.... say u are at index.html and it takes u to the normal page but inside the content i wanted a picture chosen at random from my portfolio folder.
is there a possible way i can achieve this??

thx dixon
  Reply With Quote
Old 09-04-2003, 12:40   #2 (permalink)
andrewparker
Senior Member
 
andrewparker's Avatar
 
Join Date: Feb 2003
Location: Manchester
Posts: 991
I haven't tested it but this bit of JavaScript should work.

Put this in the head of your document -

[code:1:741504306a]<script language="javascript">
<!--
var img_rnd = new Array ("image_01.gif", "image_02.gif",
"image_03.gif", "image_04.gif", "image_05.gif");
var i = Math.round(4*Math.random());

document.write('<img src="' + img_rnd[i] + '"
width="100" height="100">');

//-->
</script>
[/code:1:741504306a]

As you can see, you list the images you want to display as an array.

Change this bit -

[code:1:741504306a]var i = Math.round(4*Math.random())[/code:1:741504306a]

with however many images you have, remembering that JavaScript arrays, like PHP arrays are zero indexed. (ie. if you have 10 images insert the number 9 into the code). This bit generates a random number.

You then need to use the [i:741504306a]document.write[/i:741504306a] method where you want to display the image, hence -

[code:1:741504306a]document.write('<img src="' + img_rnd[i] + '" width="100"
height="100">');
[/code:1:741504306a]

Give me a shout if it doesn't work.....
__________________
Andrew Parker

---------------------

(Apple PowerBook G4 1.67GHz / 1Gb RAM / OS X / Safari )
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8