Click to See Complete Forum and Search --> : javascript help
yellowboy
02-16-2003, 05:27 PM
what am i doing wrong...it won't preload any images inside my for loop...what is the syntax for the preloading thing im trying to do where just the number changes and it loops? Here is the code:
<html>
<head>
<script language="javascript">
var num=1
var pict=1
var picy=60
img1 = new Image ()
img1.src = "aquarium\000_0060.jpg"
for(pict=1; pict<=20; pict=pict+1)
{
"img"+pict = new Image ()
"img"+pict+".src" = "aquarium\000_00"+picy+".jpg"
picy=picy+1
}
function slideshowUp()
{
num=num+1
if (num==5)
{num=1}
document.mypic.src=eval("img"+num+".src")
}
function slideshowBack()
{
num=num-1
if (num==0)
{num=4}
document.mypic.src=eval("img"+num+".src")
}
</script>
</head>
<body>
<IMG SRC="aquarium\000_0060.jpg" NAME="mypic" BORDER=0 height=300 width=300>
<A HREF="JavaScript:slideshowBack()"><img src="left.gif" border="none"></A>
<A HREF="JavaScript:slideshowUp()"><img src="right.gif" border="none"></A>
</body>
</html>
gil davis
02-16-2003, 06:24 PM
for(pict=1; pict<=20; pict=pict+1)
{
"img"+pict = new Image ()
"img"+pict+".src" = "aquarium\000_00"+picy+".jpg"
picy=picy+1
}
You need to read up on creating variables.
I would suggest you use an array, instead.
<script>
var num = 1;
var picy = 60;
var img = new Array();
for(pict=1; pict<=20; pict=pict+1)
{img[pict] = new Image();
img[pict].src = "aquarium\000_00" + picy + ".jpg" ;
picy = picy + 1;
}
function slideshowUp() {
num = num + 1;
if (num == 5) num = 1;
document.mypic.src = img[num].src;
}
function slideshowBack() {
num = num - 1;
if (num == 0) num = 4;
document.mypic.src = img[num].src;
}
</script>
khalidali63
02-16-2003, 06:27 PM
Originally posted by yellowboy
for(pict=1; pict<=20; pict=pict+1)
{
"img"+pict = new Image ()
"img"+pict+".src" = "aquarium\000_00"+picy+".jpg"
picy=picy+1
}
just add every image once its created in an array
var imgArray = new Array();
for(pict=1; pict<=20; pict=pict+1)
{
img = new Image ()
.src = "aquarium\000_00"+picy+".jpg";
imgArray[picy] = img
picy=picy+1
}
then you can access the unique image object with the array index.
cheers
Khalid
yellowboy
02-18-2003, 02:52 PM
I just can't get it to work...is there still anything wrong?
var num=1;
var imgArray = new Array();
var picy=62;
for(pict=1; pict<=20; pict=pict+1);
{
img = new Image ()
.src = "aquarium\000_00"+picy+".jpg";
imgArray[picy]=img
picy=picy+1;
}
function slideshowUp()
{
num=num+1;
if (num==20) num=1;
document.mypic.src=imgArray[num].src;
}
function slideshowBack()
{
num=num-1;
if (num==0) num=20;
document.mypic.src=img[num].src;
}
</script>
</head>
<body>
<p align="center"><IMG SRC="aquarium\000_0062.jpg" NAME="mypic" BORDER=0 height=300 width=300></p>
<table align="center"><tr><td><A HREF="JavaScript:slideshowBack()"><img src="left.gif" border="none"></A></td><td width=200></td><td><A HREF="JavaScript:slideshowUp()"><img src="right.gif" border="none"></A></td></tr></table>
Dan Drillich
02-18-2003, 03:33 PM
Please try -
<html>
<head>
<script language="javascript">
var num=1;
var pict=1;
var picy=60;
var img = new Array();
for(pict=1; pict<=20; pict++) {
img[pict] = eval("aquarium\000_00"+picy+".jpg)";
//img[pict] = "file:///C:/WINNT/Web/Wallpaper/Fly%20Away.jpg";
}
function slideshowUp() {
num=num+1;
if (num==5) {num=1};
document.mypic.src=img[num] ;
}
function slideshowBack() {
num=num-1;
if (num==0) {num=4};
document.mypic.src=img[num] ;
}
</script>
</head>
<body>
<IMG SRC="aquarium\000_0060.jpg" NAME="mypic" BORDER=0 height=300 width=300>
<A HREF="javascript:slideshowBack();"><img src="left.gif" border="none"></A>
<A HREF="javascript:slideshowUp();"><img src="right.gif" border="none"></A>
</body>
</html>
gil davis
02-18-2003, 04:34 PM
Originally posted by Dan Drillich
Please try -
...
img[pict] = eval("aquarium\000_00"+picy+".jpg)";
This guy wanted a preload script. This syntax (even if it was right) would not preload an image.Originally posted by khalidali63
.src = "aquarium\000_00"+picy+".jpg";This syntax is wrong, too.
Doesn't anyone test their suggestions before posting them?
Hey, yellowboy, did you try my suggestion?
yellowboy
02-18-2003, 10:54 PM
yes i tried it but it works the same as the others...only the initial image comes up...they do not change on clicking. the error says that img[...].src is not an object in the document.mypic line
AdamBrill
02-18-2003, 11:58 PM
Let me take a shot at it. :) Try this code:
<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 2</title>
<script language=javascript>
pics=new Array();
picy=60;
for(x=0;x<=5;x++)
{
pics[x]= new Image();
pics[x].src="aquarium/000_00"+picy+".jpg";
picy++;
}
num=1;
function go(sent)
{
num+=sent;
if(num>pics.length)
{
num=0;
}
if(num<0)
{
num=pics.length-1;
}
if(!pics[num])
{
num=0;
}
document.pic.src=pics[num].src;
}
</script>
</head>
<body>
<img src="aquarium/000_0060.jpg" name=pic>
<a href="javascript:go(1)">Forward</a>
<a href="javascript:go(-1)">Back</a>
</body>
</html>
Let me know if you have any problems...
yellowboy
02-19-2003, 07:46 PM
Thank you very much guys it finally works...gil your awesome...the problem seems to be that the images won't preload from a different folder
khalidali63
02-20-2003, 08:24 AM
Originally posted by gil davis
This syntax is wrong, too.
Doesn't anyone test their suggestions before posting them?
I don't see a need for that remark.In my understanding the code must not have worked for some one who does not know JavaScript.But for one who reads this forum and posts solution here,you should have the decency to use common sense( which apparently is not vey common) that it was a simple overlook or a type that I missed to put img. before the src.
I can not believe the sense of intellect around here.
cheers
Khalid
superrad7
02-20-2003, 11:09 AM
they are in the wrong place. they cant just be globally declared