Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

random pic doesn't work- Help!? 1

Status
Not open for further replies.

lisege01

Programmer
Nov 18, 2002
6
CA
hello everyone,

i cannot get this script to work. i get an error that says "causewaypics[...]src'is null or not an object". can someone please help me, i've been staring at this for too long!

thanks
=======================
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>
<html>
<head>
<title>Causeway Photo Album</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<script type=&quot;text/javascript&quot;>
<!--
causewaypics=new Array(5);
causewaypics[0]=new Image();
causewaypics[0].src=&quot;ultimate1.jpg&quot;;
causewaypics[1]=new Image();
causewaypics[1].src=&quot;ultimate2.jpg&quot;;
causewaypics[2]=new Image();
causewaypics[2].src=&quot;ultimate3.jpg&quot;;
causewaypics[3]=new Image();
causewaypics[3].src=&quot;ultimate4.jpg&quot;;
causewaypics[4]=new Image();
causewaypics[4].src=&quot;ultimate5.jpg&quot;;
causewaypics[5]=new Image();
causewaypics[5].src=&quot;xmascards.jpg&quot;;
causewaypics=new Array(&quot;ultimate1.jpg&quot;,&quot;ultimate2.jpg&quot;,&quot;ultimate3.jpg&quot;,
&quot;ultimate4&quot;,&quot;ultimate5.jpg&quot;,&quot;xmascards.jpg&quot;);
function randomPic(){
var element=Math.round(Math.random()*3);
var element2=Math.round(Math.random()*3)+4;
document.pic1.scr=causewaypics[element].src;
document.pic2.scr=causewaypics[element2].src;
setTimeout(&quot;randomPic()&quot;,2000);
}
//-->
</script>
</head>
<body onLoad=&quot;randomPic()&quot;>
<center><h3>Causeway Photo Album</h3>
<table>
<tr>
<td><img src=&quot;images/ultimate1.jpg&quot; name=&quot;pic1&quot;></td>
<td><img src=&quot;images/xmascards.jpg&quot; name=&quot;pic2&quot;></td>
</tr>
</table>
</center>
</body>
</html>
 
Hello,

1) scr isnt the same as src! :)

2)
I mean that isnt good:
document.pic1.scr=causewaypics[element].src;
document.pic2.scr=causewaypics[element2].src;

but:
document.pic1.scr=causewaypics[element];
document.pic2.scr=causewaypics[element2];

Coracal
 
Hi,
There are more streamlined solutions, but here is a working version similar to yours. I left out the timer. I think your problems were in part coming from simply adding four to the array index to get the url of your second element. Sometimes that number will exceed the array length and cause an error. You don't have to preload, either. If you ultimately don't use 4 of the images you're wasting page loading time, but it does make for a seamless opening.ALso, see xutopia'a FAQ about adding strings together for most efficient load times. (I did it the slower way, which I'm more familiar with.)

<html>
<head>
<title>Causeway Photo Album
</title>
<script language=&quot;javascript&quot;>
//array of image names/urls
var causewaypics=[&quot;ultimate1.jpg&quot;,&quot;ultimate2.jpg&quot;,&quot;ultimate3.jpg&quot;,&quot;ultimate4.jpg&quot;,&quot;ultimate5.jpg&quot;,&quot;xmascards.jpg&quot;];

//preload them if you want
var preloads=new Array();
for(var i=0;i<causewaypics.length;i++){
preloads=new Image();
preloads.src=causewaypics;
}

//create index for first image
var picIndex=Math.floor(Math.random()*causewaypics.length);

//create index for second image
var picIndex2=picIndex+4;

//correct picIndex2 if number exceeds array length
if(picIndex2>=causewaypics.length){
picIndex2-=causewaypics.length;
}

//write html string as desired
var htmStr='<table align=center><tr><td>';
htmStr+='<img src=&quot;'+causewaypics[picIndex]+'&quot;>';
htmStr+='</td><td>';
htmStr+='<img src=&quot;'+causewaypics[picIndex2]+'&quot;>';
htmStr+='</td></tr></table>';
</script>
</head>
<body onload=&quot;document.write(htmStr);&quot;>
</body>
</html>
 
On looking over your script after I posted, I see you want to auto-rotate the random images (it's late!). Now i see about the preloads. Sorry I didn't notice that. coracal is right about the urls.
 
<html>
<head>
<title>Causeway Photo Album
</title>
<script language=&quot;javascript&quot;>
var d=document;
var timerID=null;
var causewaypics=[&quot;ultimate1.jpg&quot;,&quot;ultimate2.jpg&quot;,&quot;ultimate3.jpg&quot;,&quot;ultimate4.jpg&quot;,&quot;ultimate5.jpg&quot;,&quot;xmascards.jpg&quot;];
var preloads=new Array();
for(var i=0;i<causewaypics.length;i++){
preloads=new Image();
preloads.src=&quot;images/&quot;+causewaypics;
}
function randPic(){
var picIndex=Math.floor(Math.random()*causewaypics.length);

var picIndex2=picIndex+4;
if(picIndex2>=causewaypics.length){
picIndex2-=causewaypics.length;
}
d.images[&quot;pic1&quot;].src=&quot;images/&quot;+causewaypics[picIndex];
d.images[&quot;pic2&quot;].src=&quot;images/&quot;+causewaypics[picIndex2];
timerID=setTimeout(&quot;randPic()&quot;,2000);
}
</script>
</head>
<body onload=&quot;randPic();&quot;>
<table align=center><tr><td>
<img src=&quot;images/ultimate1.jpg&quot; name=&quot;pic1&quot;>
</td><td>
<img src=&quot;images/xmascards.jpg&quot; name=&quot;pic2&quot;>
</td></tr></table>
</body>
</html>
 
hi all,

thanks for helping me, however, even after i changed scr to src (oops), it still doesnt work. i'm at my wits end.
 
i am running I.E.5 as well, but it keeps generating an error.
 
hi onpnt!

after a bit of tweeking, i got the script to work.

thank you everyone who contributed!

===========
<html>
<head>
<!-- Fireworks MX Dreamweaver MX target. Created Mon Dec 02 11:56:15 GMT-0500 (Eastern Standard Time) 2002-->
<!--<title>Code provided by sundemon at Tek-Tips</title>-->
<script language=&quot;javascript&quot;>

function describeCat(str) {
alert(str);
}
var d=document;
var timerID=null;
var causewaypics=[&quot;../images/ultimate1.jpg&quot;,&quot;../images/ultimate3.jpg&quot;,&quot;../images/ultimate5.jpg&quot;];
var preloads=new Array();
for(var i=0;i<causewaypics.length;i++){
preloads=new Image();
preloads.src=&quot;&quot;+causewaypics;
}
function randPic(){
var picIndex=Math.floor(Math.random()*causewaypics.length);

var picIndex2=picIndex+2;
if(picIndex2>=causewaypics.length){
picIndex2-=causewaypics.length;
}
d.images[&quot;pic1&quot;].src=&quot;&quot;+causewaypics[picIndex];
d.images[&quot;pic2&quot;].src=&quot;&quot;+causewaypics[picIndex2];
timerID=setTimeout(&quot;randPic()&quot;,2000);
}
</script>
</head>
<body onload=&quot;randPic();&quot;>

<table align=center width=100%>
<!--DWLayoutTable-->
<tr>
<td width=&quot;23&quot; height=&quot;22&quot; valign=&quot;top&quot;><img src=&quot;../images/ultimate2.jpg&quot; width=&quot;112&quot; height=&quot;96&quot; name=&quot;pic1&quot;></td>
<td width=&quot;143&quot;> </td>
<td><img src=&quot;../images/ultimate4.jpg&quot; width=&quot;110&quot; height=&quot;285&quot; name=&quot;pic2&quot;></td>
</tr>
</table>
</body>
</html>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top