| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
|
|
#1 (permalink) |
|
Custom User Title
Join Date: Apr 2003
Location: Brighton
Posts: 589
|
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 |
|
|
|
|
|
#2 (permalink) |
|
Senior Member
Join Date: Feb 2003
Location: Manchester
Posts: 980
|
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 ) |
|
![]() |