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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem using a js file 3

Status
Not open for further replies.

snuv

Programmer
Oct 30, 2001
751
GB
I am trying to display pictures on a simple website
and want a next button

Rather than have an html file for each picture and have to maintain all the links, I want to use an array and update the image when the user clicks on the next button

I will have more than one of these galleries so want to store the function to find the next image in the list in a js file but have the array defined in the html file.

When I define it as below it doesnt work, but when I move the array declaraction and NextPic() function into the js file it does.

Can anyone tell me what I'm doing wrong.


test.html
Code:
<SCRIPT LANGUAGE="JavaScript" src="test2.js">
var PictureArray=new Array();
PictureArray[0]="1808.jpg";
PictureArray[1]="1815.jpg";
PictureArray[2]="1766.jpg";
PictureArray[3]="1767.jpg";
PictureArray[4]="1789.jpg";

currentPicture="1808.jpg";


function NextPic()
{
	currentPicture=NextPicture(PictureArray,currentPicture);
	alert(currentPicture);
}
</SCRIPT>


test2.js
Code:
function NextPicture(lst,CurrentPic)
{
	limit = lst.length;
	RetVal = "";
	for (i=0;i<limit;i++)
	{
		pic = lst[i];
		if (pic==CurrentPic)
		{
			if (i==limit-1)
			{
				RetVal = lst[0];
			}
			else
			{
				RetVal = lst[i+1];
			}
			i=limit;
		}
	}
	return RetVal;
}

Thanks in advance
Snuv

"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary
 
Here's a slideshow that I use frequently that you should be able to easily adapt to your needs. It has some things you can eliminate, like the automatic slideshow and the captions, if you don't want those features.


Lee
 
Thanks trollacious

That showed me exactly what I needed to do

cheers
Snuv

"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary
 
snuv, I'll throw this in so that you know for the next time, as it was likely the reason that your page was crashing in the first place. This happened to me shortly after I got my first programming job with javascript, and it took me about 2 weeks to figure out what was going on. Since then I've always look for it when ppl are having trouble with .js files.

The script tag you posted above has this little bit in it:
Code:
<SCRIPT LANGUAGE="JavaScript" [!]src="test2.js"[/!]>
var PictureArray=new Array();
PictureArray[0]="1808.jpg";
PictureArray[1]="1815.jpg";
PictureArray[2]="1766.jpg";
PictureArray[3]="1767.jpg";
PictureArray[4]="1789.jpg";

currentPicture="1808.jpg";


function NextPic()
{
    currentPicture=NextPicture(PictureArray,currentPicture);
    alert(currentPicture);
}
</SCRIPT>

When you define an external .js file in your <script> tag, the <script> tag will ignore the code that you put w/in the <script></script> tags. You can either import a js file in the <script> tag, or put the code between the <script></script> tags - but not both. So, to make your old code work you'd have to have an extra <script> tag with the sole purpose of importing the external js file:
Code:
[!]<script type="text/javascript" src="test2.js"></script>[/!]
<script type="text/javascript">
var PictureArray=new Array();
PictureArray[0]="1808.jpg";
PictureArray[1]="1815.jpg";
PictureArray[2]="1766.jpg";
PictureArray[3]="1767.jpg";
PictureArray[4]="1789.jpg";

currentPicture="1808.jpg";


function NextPic()
{
    currentPicture=NextPicture(PictureArray,currentPicture);
    alert(currentPicture);
}
</script>

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Thanks Kaht

Thats the valuable lesson I learnt from trollacious but you've saved me the job of writing it up

Cheers
Snuv

"If it could have gone wrong earlier and it didn't, it ultimately would have been beneficial for it to have." : Murphy's Ultimate Corollary
 
Star for Kaht, I just spent 3 days on the same freaking thing!!! thanks for saving me 11 :)

--Dan
Whenever you find yourself on the side of the majority, it is time to pause and reflect.
Mark Twain
 
Star for Kaht, I just spent 3 days on the same freaking thing!!! thanks for saving me 11 [smile]

Not a problem. [thumbsup2]



A quick bit of kaht-trivia (if such a thing exists...) When I first joined TT back in '03 I had only been using javascript for about 2 weeks. My very first question posted to TT was about this very problem. I didn't get it solved from the thread I posted, though. I finally figured out what was wrong about a month later by playing around with it...

thread216-632043

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top