Searched the forums and the site to "randomize" a set of pictures, have one picture change everytime the page is hit
Thank you!
Searched the forums and the site to "randomize" a set of pictures, have one picture change everytime the page is hit
Thank you!
Something like this? http://www.jsmadeeasy.com/javascript...s/template.htm
Otherwise, try www.google.com .
cr3ative
A retired member, drop me a line through my site if you'd like to find me!
cr3ative media | read the stickies
just rename the shotx.jpgCode:<script language='javascript'> //-- Generate a random number between 0 and max. function randomNum(max) { var rNum=NaN while (isNaN(rNum)) { rNum=Math.floor(Math.random()*(max)) } return rNum } var pic = new Array() pic[0]="shot01.jpg" pic[1]="shot02.jpg" pic[2]="shot03.jpg" pic[3]="shot04.jpg" pic[4]="shot05.jpg" pic[5]="shot06.jpg" pic[6]="shot07.jpg" pic[7]="shot08.jpg" pic[8]="shot09.jpg" pic[9]="shot10.jpg" pic[10]="shot11.jpg" pic[11]="shot12.jpg" pic[12]="shot13.jpg" pic[13]="shot14.jpg" pic[14]="shot15.jpg" </script>
then
for the picCode:<script language='javascript'> imgTag='<img border="0" src="' imgTag+=pic[randomNum(pic.length)] imgTag+='" align="left" hspace="0"' imgTag+=' width="200" height="150"' document.write (imgTag) </script>
change the align, width and all that crap. this should work, it works for me, tweak it if you have to.
Please use up-to-date mark-up. Use of the language attribute should have ended over six years ago.Originally Posted by darco9x2
is sufficient in all cases.HTML Code:<script type="text/javascript">
I'm curious: why do you think that will produce NaN? Unless the argument max is not a number, I don't see how that could happen. However, even then the code will go into an infinite loop so it should be the responsibility of the caller to make sure the argument is valid.Code:while (isNaN(rNum)) { rNum=Math.floor(Math.random()*(max)) }
Code:/* Generates a pseudo-random integer * in the range o <= x < (n + o) * where o defaults to zero (0). */ function random(n, o) {o = +o || 0; return Math.floor((Math.random() % 1) * n) + o; }It's generally better to use an array literal to create an initialised array:Code:var pic = new Array() [...]
Code:var pic = [ 'shot01.jpeg', 'shot02.jpeg', /* ... */ 'shot15.jpeg' ];If you don't want borders on images, use CSS:Code:imgTag='<img border="0"
It's far more efficient. Dump deprecated attributes like align and hspace. HTML 3.2 is dead.Code:img { border-style: none; }
Mike
k thanks
Hi everybody,
just found your thread and noticed that all of you got stuck on the idea of using an array for picking a random picture. Here is a (shorter) solution without having to set up and fill an array:
Code:<script type="text/javascript"> function randpic(pic,n) { // picks a random picture // pic: path to pictures and first (common) part of picture name // each picture carries a 3-digit number from 1 to n and ends with ".jpg" var i=String(1000+Math.ceil(Math.random()*n)).substr(1) document.writeln("<img src=\""+pic+i+".jpg\" alt=\"Bild Nr. "+i+"\">") } </script>
Bookmarks