View Full Version : help with my image random script?
CeeNuh
08-06-2003, 11:10 PM
hey, I have a perfect random image script, but sometimes the same image apears twice before the whole lot has been shown. Is there any way to correct this?
Here's my script:
<SCRIPT>
{img=new Array()
img[0]="http://sarchasm.digitalrice.com/2.jpg"
img[1]="http://sarchasm.digitalrice.com/3.jpg"
img[2]="http://sarchasm.digitalrice.com/4.jpg"
img[3]="http://sarchasm.digitalrice.com/1.jpg"
img[4]="http://sarchasm.digitalrice.com/5.jpg"
img[5]="http://sarchasm.digitalrice.com/6.jpg"
img[6]="http://sarchasm.digitalrice.com/7.jpg"
img[7]="http://sarchasm.digitalrice.com/8.jpg"
img[8]="http://sarchasm.digitalrice.com/9.jpg"
img[9]="http://sarchasm.digitalrice.com/10.jpg"
img[10]="http://sarchasm.digitalrice.com/11.jpg"
img[11]="http://sarchasm.digitalrice.com/12.jpg"
img[12]="http://sarchasm.digitalrice.com/13.jpg"
img[13]="http://sarchasm.digitalrice.com/14.jpg"
mrnd = Math.floor(Math.random() * img.length)
document.writeln('<IMG SRC="'+img[mrnd]+'">')}
</SCRIPT>
fredmv
08-06-2003, 11:29 PM
There's no point in having your code between curelybraces, that's usually to show code blocks...
But anyway, the main way this could be done is setting a cookie of the last randomly displayed image, then before you display a new one, check the cookie, if they match, generate another random number and pick a new image URL out of the array. If they don't, display the image.
Good luck.
CeeNuh
08-07-2003, 10:02 AM
I see. Hmm where would I start if I wanted to make it cookie based?
glenngv
08-07-2003, 10:27 AM
Create your own cookie script or look for one (http://www.codingforums.com/showthread.php?s=&threadid=11585).
Please try the following.
<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
</HEAD>
<BODY>
<script language="javascript">
<!--
// realise by apachejeff
// www.huntingground.freeserve.co.uk
image = new Array
image[image.length] = "pic1.jpg"
image[image.length] = "pic2.jpg"
image[image.length] = "pic3.jpg"
image[image.length] = "pic4.jpg"
image[image.length] = "pic5.jpg"
image[image.length] = "pic6.jpg"
Numbers_Range=new Array() // array to hold numbers to select from.
function init(){
for(n=0;n<image.length;n++){ // create list of numbers to choose from
Numbers_Range[n]=n // start of numbers to choose from
}
show_pic()
}
num=0
function show_pic(){
num++
rndnum=Math.round(Math.random()*(Numbers_Range.length-1))
Chosen_Number=Numbers_Range.splice(rndnum,1) // select and remove selected number from array
document.mypic.src=image[Chosen_Number]
timer=setTimeout("show_pic()",3000)
if(num>image.length){
clearTimeout(timer)
num=0
init()
}
}
setTimeout("init()",1000) // set onload delay
// -->
</script>
<img src="pic0.jpg" name="mypic" width=100 height=100>
</BODY>
</HTML>
CeeNuh
08-07-2003, 06:46 PM
I tried your script Mr J, and it just gave me an error looking for document.image
<script language="javascript">
<!--
// realise by apachejeff
// www.huntingground.freeserve.co.uk
image = new Array
image[image.length] = "http://sarchasm.digitalrice.com/1.jpg"
image[image.length] = "http://sarchasm.digitalrice.com/2.jpg"
image[image.length] = "http://sarchasm.digitalrice.com/3.jpg"
image[image.length] = "http://sarchasm.digitalrice.com/4.jpg"
image[image.length] = "http://sarchasm.digitalrice.com/5.jpg"
image[image.length] = "http://sarchasm.digitalrice.com/6.jpg"
image[image.length] = "http://sarchasm.digitalrice.com/7.jpg"
image[image.length] = "http://sarchasm.digitalrice.com/8.jpg"
image[image.length] = "http://sarchasm.digitalrice.com/9.jpg"
image[image.length] = "http://sarchasm.digitalrice.com/10.jpg"
image[image.length] = "http://sarchasm.digitalrice.com/11.jpg"
image[image.length] = "http://sarchasm.digitalrice.com/12.jpg"
image[image.length] = "http://sarchasm.digitalrice.com/13.jpg"
image[image.length] = "http://sarchasm.digitalrice.com/14.jpg"
Numbers_Range=new Array() // array to hold numbers to select from.
function init(){
for(n=0;n<image.length;n++){ // create list of numbers to choose from
Numbers_Range[n]=n // start of numbers to choose from
}
show_pic()
}
num=0
function show_pic(){
num++
rndnum=Math.round(Math.random()*(Numbers_Range.length-1))
Chosen_Number=Numbers_Range.splice(rndnum,1) // select and remove selected number from array
document.mypic.src=image[Chosen_Number]
timer=setTimeout("show_pic()",3000)
if(num>image.length){
clearTimeout(timer)
num=0
init()
}
}
setTimeout("init()",1000) // set onload delay
// -->
</script>
You forgot to include the following line in the body section of your page
<img src="pic0.jpg" name="mypic">
:o
PS
I have tried it in IE5.5 NS7 and Mozilla
What browser are you using?
cheesebag
08-08-2003, 03:42 AM
This is a once-per-reload thing, right?
<html>
<head>
<title>untitled</title>
</head>
<body>
<script type="text/javascript" language="javascript">
if (!/^raNdOm_IMagEs:/.test(self.name)) {
numarray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];
var idx1, idx2, i = 0;
for (i; i < numarray.length; i++) {
idx1 = Math.floor(Math.random() * numarray.length);
idx2 = Math.floor(Math.random() * numarray.length);
temp = numarray[idx1];
numarray[idx1] = numarray[idx2];
numarray[idx2] = temp;
}
} else {
tempstr = self.name.split('raNdOm_IMagEs:')[1];
numarray = tempstr.split('|');
}
num = numarray.splice(0, 1);
self.name = 'raNdOm_IMagEs:' + numarray.concat(num).join('|');
document.write('<img border="0" src="http://sarchasm.digitalrice.com/' + num + '.jpg">');
</script>
</body>
</html>
glenngv
08-08-2003, 03:59 AM
I know that window.name trick. It's very useful for saving persistent data except if you're using it in a framed page where that frame is used as link targets.
cheesebag
08-08-2003, 04:05 AM
I know that "I know that window.name trick" trick. :D
Hey, we'll cross that (framed) bridge when we come to it.
stoney
03-27-2004, 07:57 PM
[FONT=times new roman]JUST WHEN I WAS IN THE MIDDLE OF AN UPLOAD OF THE DANG ZIP FILE.....A QWEST MAN STOPS BY AND UNPLUGS ME FROM A BOX OUT FRONT OF MY HOUSE!
I HATE WHEN THAT HAPPENS. SO I'LL TRY AND UPLOAD IT AGAIN!
stoney
03-27-2004, 08:05 PM
OK - I SEE MY FIRST POST DIDN'T MAKE IT : THE FIRST POST WAS:
THIS LITTLE ITEM THAT ADIOS MADE FOR ME IS GREAT!
BUT....IS THERE A COOKIE SCRIPT I CAN PUT INTO THE DARN THING SO MY IMAGES DON'T REPEAT AFTER THE FIRST, SECOND, AND THIRD TRIES? I HAVE 52 IMAGES I'M USING: WHAT'S HAPPENING IS AT LEAST 2 OUT OF 10 IN THE TABLE ARE REPEATING --- BUT THEY SHOW UP IN A DIFFERENT SPOT ON THE TABLE OF 10 PLACEMENTS.
SO WHAT I WANT TO DO IS MAKE IT SO IT RANDOMLY SELECTS THESE 52 IMAGES AND DOESN'T MAKE IT SO PREDICTABLE THAT I'M GOING TO BE SEEING AT LEAST 2 OUT OF THE LAST 10 IMAGES POPPING UP.
I SAW YOUR POST ON COOKIES AND I'M ASSUMING THAT'S WHAT MY PROBLEM IS WITH THIS SCRIPT. BTW: I DON'T KNOW ANYTHING ABOUT JAVA SO BARE WITH ME....
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.