...

View Full Version : multiple random image scripts on one page - possible??



badmix
10-21-2003, 04:02 AM
using this script http://www.javascriptkit.com/script/cut118.shtml
can i have more than one in different locations on the same page? nothing i've tried works but i'm no javascript expert....

oracleguy
10-21-2003, 07:13 AM
Untested but it should work.



<script language="JavaScript">
<!--

/*
Random Image Script- By JavaScript Kit (http://www.javascriptkit.com)
Over 400+ free JavaScripts here!
Keep this notice intact please
*/

function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="image1.gif"
myimages[2]="image2.gif"
myimages[3]="image3.gif"
myimages[4]="image4.gif"
myimages[5]="image5.gif"
myimages[6]="image6.gif"

for (i = 0; i < 3; i++) {
var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<img src="'+myimages[ry]+'" border=0>')
}
}
random_imglink()
//-->
</script>


If you want the images to be in other locations that all together the script could be modified so it assigns images with a certain id a random image. So you could have the images scattered on the page and then just running the function would assign them random images.

badmix
10-21-2003, 08:06 AM
ok, talk to me like i'm four. i'm not getting that to work.
do i list all the random images in one place? say i want 5 images shown at once -
how do i tell it to pick from random1.jpg to random10.jpg for the first random image,
11-20 for the second, 21-30 for the third etc.?

what i tried before i posted was using 5 copies of the script, adding a number to 'myimages' and 'random_imglink'
so i had 'myimages1', 'myimages2', 'random_imglink1', 'random_imglink2' etc. it would load random images in the 5 places TOO randomly!
sometimes 3 would show up with the other two showing the broken image thing, sometimes only one would show,
sometimes 2, and about every 50th reload all five would show up just fine.

glenngv
10-21-2003, 10:25 AM
try this:


<script language="javascript">
<!--
var gallery = new Array();
var gallery[0] = new Array("image1.gif","image2.gif","image3.gif");
var gallery[1] = new Array("image4.gif","image5.gif","image6.gif");
var gallery[2] = new Array("image7.gif","image8.gif","image9.gif");

function pickImageFrom(whichGallery){
var idx = Math.floor(Math.random()*gallery[whichGallery].length);
document.write('<img src="'+gallery[whichGallery][idx]+'" border=0>');
}
//-->
</script>
...
<body>
...
<script language="javascript">pickImageFrom(0);</script>
...
<script language="javascript">pickImageFrom(1);</script>
...
<script language="javascript">pickImageFrom(2);</script>
...
</body>

badmix
10-21-2003, 05:18 PM
i can't get it to work with that code either. no images show up, not even the broken image thing.

oracleguy
10-21-2003, 11:57 PM
I think it is time for you to give us a link to the page where it isn't working.

It will be a lot easier to determine what is breaking it.

badmix
10-22-2003, 12:16 AM
here it is: http://www.how2llc.com/test/random.html

for anyone wanting to know how to get it to work, this works perfectly:

<script language="javascript">
<!--
var gallery = new Array();
gallery[0] = new Array("01.jpg","02.jpg","03.jpg","04.jpg","05.jpg");
gallery[1] = new Array("06.jpg","07.jpg","08.jpg","09.jpg","10.jpg");
gallery[2] = new Array("11.jpg","12.jpg","13.jpg","14.jpg","15.jpg");
gallery[3] = new Array("16.jpg","17.jpg","18.jpg","19.jpg","20.jpg");
gallery[4] = new Array("21.jpg","22.jpg","23.jpg","24.jpg","25.jpg");

function pickImageFrom(whichGallery)
{
var idx = Math.floor(Math.random() * gallery[whichGallery].length);
document.write('<img src="images/random/' + gallery[whichGallery][idx] + '">');
}
//-->
</script>
</head>
<body>

<div align="center"><script language="javascript">pickImageFrom(0);</script><br /><br />
<script language="javascript">pickImageFrom(1);</script><br /><br />
<script language="javascript">pickImageFrom(2);</script><br /><br />
<script language="javascript">pickImageFrom(3);</script><br /><br />
<script language="javascript">pickImageFrom(4);</script>
</div>

thanks for the help!

glenngv
10-22-2003, 06:13 AM
it works for me..random images showed up from corresponding galleries. What browser are you using?

badmix
10-22-2003, 06:28 AM
yeah, the last code i posted is working perfectly. thanks for checking it!!

wissenwill
01-28-2009, 05:40 PM
how can I add a 'specific' link to each image?

wissenwill
01-29-2009, 10:39 AM
Found a solution with links, but...



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN">

<html>
<head>
<script type="text/javascript">
var gallery = new Array();
gallery[0] = new Array(new Array ("module_01.jpg", "#01"), new Array ("module_02.jpg", "#02"), new Array ("module_03.jpg", "#03"), new Array ("module_04.jpg", "#04"));
gallery[1] = new Array(new Array ("module_05.jpg", "#05"), new Array ("module_06.jpg", "#06"), new Array ("module_07.jpg", "#07"), new Array ("module_08.jpg", "#08"));


function pickImageFrom(whichGallery)
{
var idx = Math.floor(Math.random() * gallery[whichGallery].length);

document.write('<a href="' + gallery[whichGallery][idx][1] + '"><img src="images/' + gallery[whichGallery][idx][0] + '"></a>');
}
</script>
</head>
<body>

<script language="javascript">pickImageFrom(0);</script><br /><br />
<script language="javascript">pickImageFrom(1);</script><br /><br />
</body>
</html>


...it looks crapy... How can I do this better? Some small help from a professional would be great! THX!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum