954,593 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with Javascript please - refresh images with links

Hi,

Using Dreamweaver, I'm trying to implement code on my page so that the images refresh upon loading, and each image links to a different page....

I'm also trying to have it so images will rotate on the page and link to other pages within my site.

please help!

email: >email snipped<
thank you!

rowads80
Newbie Poster
3 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

As far as I understand, what you need is 1) to create a functions that is called every time your page is loaded; tht's achieved with the onload="myfunction()" attribute within the tag. and 2) the function needs to hold an array of pictures that can be presented randomly.
Am I correct? How much programming experience you have?

HenryGR
Newbie Poster
24 posts since Mar 2008
Reputation Points: 10
Solved Threads: 4
 

1) to create a functions that is called every time your page is loaded; tht's achieved with the onload="myfunction()" attribute within the tag. and 2) the function needs to hold an array of pictures that can be presented randomly.
Am I correct?

that is correct...and I need to have the pictures link to .html pages within my site.

How much programming experience you have?

very little. i'm a beginner.

rowads80
Newbie Poster
3 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

OK!, I wrote a simple code that you may customize. It is created for 10 rolling pictures, but the number can easily be incremented just by adding more picture definition lines, the code will automatically adapt.

<!DOCTYP-E html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My page</title>
<script language="JavaScript" type="text/javascript">
	function mainelem(src,texto,url,targ,ancho) {
		this.src	= src;
		this.texto = texto;
		this.url = url;
		this.targ =	targ;
		this.ancho = ancho;
	}
//
	var	elemActual = 0;
	var	topeElems =	100;
	var	i =	0;
	var	numElementos = 0;
	var	elementos =	new	Array(topeElems);
	elementos[i] = new mainelem("picture1.jpg","Alt text for pic1","linkto1.htm","_self",250); i++
	elementos[i] = new mainelem("picture2.jpg","Alt text for pic2","linkto2.htm","_self",250); i++
	elementos[i] = new mainelem("picture3.jpg","Alt text for pic3","linkto3.htm","_self",250); i++
	elementos[i] = new mainelem("picture4.jpg","Alt text for pic4","linkto4.htm","_self",250); i++
	elementos[i] = new mainelem("picture5.jpg","Alt text for pic5","linkto5.htm","_self",250); i++
	elementos[i] = new mainelem("picture6.jpg","Alt text for pic6","linkto6.htm","_self",250); i++
	elementos[i] = new mainelem("picture7.jpg","Alt text for pic7","linkto7.htm","_self",250); i++
	elementos[i] = new mainelem("picture8.jpg","Alt text for pic8","linkto8.htm","_self",250); i++
	elementos[i] = new mainelem("picture9.jpg","Alt text for pic9","linkto9.htm","_self",250); i++
	elementos[i] = new mainelem("picture0.jpg","Alt text for pic0","linkto0.htm","_self",250); i++
	numElementos = i;

	function setnextcontent() {
		var	indice;
		var	aux;

		if (elemActual >= (numElementos	- 1)) {
			indice = 0;
		}
		else {
			indice = parseInt(elemActual) +	1;
		}
		elemActual = indice;
		var	elem = elementos[indice];
		document.getElementById("fotoPpal").src	= elem.src;
		document.getElementById("fotoPpal").alt	= elem.texto;
		document.getElementById("fotoPpal").width = elem.ancho;
		document.getElementById("refPpal").href	= elem.url;
		document.getElementById("refPpal").target =	elem.targ;
		document.getElementById("masInfo").innerHTML = elem.texto;
	}
	function ponContenidoPpalRandom() {
		var	indice;
		var	aux;

		indice = Math.round(Math.random()*(numElementos-1))+0;
		elemActual = indice;

		var	elem = elementos[indice];
		document.getElementById("fotoPpal").src	= elem.src;
		document.getElementById("fotoPpal").alt	= elem.texto;
		document.getElementById("fotoPpal").width = elem.ancho;
		document.getElementById("refPpal").href	= elem.url;
		document.getElementById("refPpal").target =	elem.targ;
		document.getElementById("masInfo").innerHTML = elem.texto;
	}
</script>
<body id="homebody" onLoad="ponContenidoPpalRandom();">
<div align="center">
  <div align="center" style="width:250px; overflow:hidden; "> <a id="refPpal" href="#" target="_blank" ><img id="fotoPpal" alt="default image" border="0" width="250" src="default.jpg" /></a> </div>
  <div id="masInfo" class="bodytext" align="center">
    <!-- Photo caption -->
    &nbsp;</div>
  <script language="JavaScript"	type="text/javascript">setInterval("setnextcontent();",5000);</script>
</div>
</body>
</html>


the onLoad="ponContenidoPpalRandom();" within the body tag, will show one randomly selected picture at every load and pictures will hence appear consecutively every half a second.

Hope this helps.

HenryGR
Newbie Poster
24 posts since Mar 2008
Reputation Points: 10
Solved Threads: 4
 

This was almost exactly what I was looking for!!

However I don't want the pictures to change every half second just when the page is refreshed. Not very good with coding, but I took out a bit of coding that i thought was right to eliminate to make a new picture only appear when the page is reloaded.
I'm also trying to make the text underneath the picture to link...but i'm only destroying the code when i do that...
Can anybody help me alter this code so
1) The pictures are not changing every half second
2) Make the the text underneath the picture link as well as the picture.

Thanks a lot!!

kaha
Newbie Poster
2 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

Refactoring Henry's code into a more familiar pattern (and building in a bit of flexibility), we get this:

<script language="JavaScript" type="text/javascript">
var	randomImage = function(){
	var	elementos = [];
	return {
		add : function(src, texto, url, targ, ancho){
			elementos.push({src:src, texto:texto, url:url, targ:targ, ancho:ancho});
		},
		setElemIDs : function(linkID, imgID, captionID){
			this.linkElem = (document.getElementById) ? document.getElementById(linkID) : document.all[linkID];
			this.imgElem = (document.getElementById) ? document.getElementById(imgID) : document.all[imgID];
			this.captionElem = (document.getElementById) ? document.getElementById(captionID) : document.all[captionID];
		},
		random : function(interval, captionLink){
			interval = (!interval) ? null : interval;
			captionLink = (!captionLink) ? false : true;
			indice = Math.min( Math.floor(Math.random()*(elementos.length)), elementos.length-1 );
			if(this.linkElem){
				this.linkElem.href = elementos[indice].url;
				this.linkElem.target = elementos[indice].targ;
			}
			if(this.imgElem){
				this.imgElem.src = elementos[indice].src;
				this.imgElem.alt = elementos[indice].texto;
				this.imgElem.width = elementos[indice].ancho;
			}
			if(this.captionElem){
				if(captionLink) {
					this.captionElem.firstChild.href = elementos[indice].url;
					this.captionElem.firstChild.innerHTML = elementos[indice].texto;
				}
				else{
					this.captionElem.innerHTML = elementos[indice].texto;
				}
			}
			if(interval !== null) { setInterval('randomImage.random(' + interval + ', ' + captionLink + ');', interval); }
		}
	};
}();
randomImage.add("picture1.jpg", "Alt text for pic1", "linkto1.htm", "_self", 250);
randomImage.add("picture2.jpg", "Alt text for pic2", "linkto2.htm", "_self", 250);
randomImage.add("picture3.jpg", "Alt text for pic3", "linkto3.htm", "_self", 250);
randomImage.add("picture4.jpg", "Alt text for pic4", "linkto4.htm", "_self", 250);
randomImage.add("picture5.jpg", "Alt text for pic5", "linkto5.htm", "_self", 250);
randomImage.add("picture6.jpg", "Alt text for pic6", "linkto6.htm", "_self", 250);
randomImage.add("picture7.jpg", "Alt text for pic7", "linkto7.htm", "_self", 250);
randomImage.add("picture8.jpg", "Alt text for pic8", "linkto8.htm", "_self", 250);
randomImage.add("picture9.jpg", "Alt text for pic9", "linkto9.htm", "_self", 250);
randomImage.add("picture0.jpg", "Alt text for pic0", "linkto0.htm", "_self", 250);

onload = function(){
	randomImage.setElemIDs("refPpal", "fotoPpal", "masInfo");
	randomImage.random(null, false);
}
</script>

The great thing now is that you can control the behaviour without having to rewrite the code.

See the line right at the bottom, randomImage.random(null, false); . That's the flexible bit. The parameters are as follows:

randomImage.random(interval, captionLink);<ul><li><strong>interval</strong> : time in milliseconds between automatic "rollovers". Set to  <code>null</code>  for no rollover.</li>
<li><strong>captionLink</strong> : true = caption is a clickable link; false = caption is text only.</li>
</ul>

Examples: randomImage.random(null, false); //No rollovers, text-only caption
randomImage.random(5000, false); //5 second rollovers, text-only caption
randomImage.random(null, true); //No rollovers, caption is clickable
randomImage.random(7000, true); //7 second rollovers, caption is clickable
Should keep everyone happy. Well, I doubt it but here's hoping.

Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

Oh great this is perfect! Thanks Airshow.
I actually found another code that I used cause I had to get it working so quickly. It works pretty good, the only thing is that the caption under the picture doesn't link, but the neat thing is that it is short and sweet. So that had to do for now. But I might update now with your help here.
Anyways if anyone would like to use the code I used it's here:

<script type="text/javascript">
var img_rnd = new Array ("image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg");
var text_rnd = new Array ("Text 1", " Text 2", " Text 3", "Text 4");
var i = Math.round(3 * Math.random());
     
document.write('<a href=link.html><img class=border src="' + img_rnd[i] + '"</a><br/>' + text_rnd[i]);
</script>

Refactoring Henry's code into a more familiar pattern (and building in a bit of flexibility), we get this:

<script language="JavaScript" type="text/javascript">
var	randomImage = function(){
	var	elementos = [];
	return {
		add : function(src, texto, url, targ, ancho){
			elementos.push({src:src, texto:texto, url:url, targ:targ, ancho:ancho});
		},
		setElemIDs : function(linkID, imgID, captionID){
			this.linkElem = (document.getElementById) ? document.getElementById(linkID) : document.all[linkID];
			this.imgElem = (document.getElementById) ? document.getElementById(imgID) : document.all[imgID];
			this.captionElem = (document.getElementById) ? document.getElementById(captionID) : document.all[captionID];
		},
		random : function(interval, captionLink){
			interval = (!interval) ? null : interval;
			captionLink = (!captionLink) ? false : true;
			indice = Math.min( Math.floor(Math.random()*(elementos.length)), elementos.length-1 );
			if(this.linkElem){
				this.linkElem.href = elementos[indice].url;
				this.linkElem.target = elementos[indice].targ;
			}
			if(this.imgElem){
				this.imgElem.src = elementos[indice].src;
				this.imgElem.alt = elementos[indice].texto;
				this.imgElem.width = elementos[indice].ancho;
			}
			if(this.captionElem){
				if(captionLink) {
					this.captionElem.firstChild.href = elementos[indice].url;
					this.captionElem.firstChild.innerHTML = elementos[indice].texto;
				}
				else{
					this.captionElem.innerHTML = elementos[indice].texto;
				}
			}
			if(interval !== null) { setInterval('randomImage.random(' + interval + ', ' + captionLink + ');', interval); }
		}
	};
}();
randomImage.add("picture1.jpg", "Alt text for pic1", "linkto1.htm", "_self", 250);
randomImage.add("picture2.jpg", "Alt text for pic2", "linkto2.htm", "_self", 250);
randomImage.add("picture3.jpg", "Alt text for pic3", "linkto3.htm", "_self", 250);
randomImage.add("picture4.jpg", "Alt text for pic4", "linkto4.htm", "_self", 250);
randomImage.add("picture5.jpg", "Alt text for pic5", "linkto5.htm", "_self", 250);
randomImage.add("picture6.jpg", "Alt text for pic6", "linkto6.htm", "_self", 250);
randomImage.add("picture7.jpg", "Alt text for pic7", "linkto7.htm", "_self", 250);
randomImage.add("picture8.jpg", "Alt text for pic8", "linkto8.htm", "_self", 250);
randomImage.add("picture9.jpg", "Alt text for pic9", "linkto9.htm", "_self", 250);
randomImage.add("picture0.jpg", "Alt text for pic0", "linkto0.htm", "_self", 250);

onload = function(){
	randomImage.setElemIDs("refPpal", "fotoPpal", "masInfo");
	randomImage.random(null, false);
}
</script>

The great thing now is that you can control the behaviour without having to rewrite the code.

See the line right at the bottom, randomImage.random(null, false); . That's the flexible bit. The parameters are as follows:

randomImage.random(interval, captionLink);<ul><li><strong>interval</strong> : time in milliseconds between automatic "rollovers". Set to  <code>null</code>  for no rollover.</li>
<li><strong>captionLink</strong> : true = caption is a clickable link; false = caption is text only.</li>
</ul>

Examples:

  • randomImage.random(null, false); //No rollovers, text-only caption
  • randomImage.random(5000, false); //5 second rollovers, text-only caption
  • randomImage.random(null, true); //No rollovers, caption is clickable
  • randomImage.random(7000, true); //7 second rollovers, caption is clickable

Should keep everyone happy. Well, I doubt it but here's hoping.

Airshow

kaha
Newbie Poster
2 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

You might want to look into setInterval() to get things to happen every second. :)

JugglerDrummer
Junior Poster
139 posts since Apr 2009
Reputation Points: 14
Solved Threads: 22
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You