| | |
Help with Javascript please - refresh images with links
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Apr 2008
Posts: 3
Reputation:
Solved Threads: 0
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!
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!
Last edited by MattEvans; Apr 7th, 2008 at 5:51 am. Reason: removed email address
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 <body> 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?
Am I correct? How much programming experience you have?
You keep going, have a Nice day!
Henry.
Before printing this message, make sure is necessary.
Henry.
Before printing this message, make sure is necessary.
•
•
Join Date: Apr 2008
Posts: 3
Reputation:
Solved Threads: 0
1) to create a functions that is called every time your page is loaded; tht's achieved with the onload="myfunction()" attribute within the <body> 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.
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.
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.
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.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!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 --> </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.
You keep going, have a Nice day!
Henry.
Before printing this message, make sure is necessary.
Henry.
Before printing this message, make sure is necessary.
•
•
Join Date: May 2009
Posts: 2
Reputation:
Solved Threads: 0
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!!
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!!
Refactoring Henry's code into a more familiar pattern (and building in a bit of flexibility), we get this:
The great thing now is that you can control the behaviour without having to rewrite the code.
See the line right at the bottom,
Examples:
Airshow
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<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>
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);
- interval : time in milliseconds between automatic "rollovers". Set to
nullfor no rollover. - captionLink : true = caption is a clickable link; false = caption is text only.
-
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
Airshow
•
•
Join Date: May 2009
Posts: 2
Reputation:
Solved Threads: 0
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:
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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<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:
The great thing now is that you can control the behaviour without having to rewrite the code.JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<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>
See the line right at the bottom,randomImage.random(null, false);. That's the flexible bit. The parameters are as follows:
Examples:randomImage.random(interval, captionLink);
- interval : time in milliseconds between automatic "rollovers". Set to
nullfor no rollover.- captionLink : true = caption is a clickable link; false = caption is text only.
Should keep everyone happy. Well, I doubt it but here's hoping.
randomImage.random(null, false);//No rollovers, text-only captionrandomImage.random(5000, false);//5 second rollovers, text-only captionrandomImage.random(null, true);//No rollovers, caption is clickablerandomImage.random(7000, true);//7 second rollovers, caption is clickable
Airshow
Last edited by peter_budo; May 17th, 2009 at 4:32 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
![]() |
Similar Threads
- XML Bug Error. (XML, XSLT and XPATH)
- Having a bit of trouble figuring out my image upload script (PHP)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: freedns.afraid.org and cross-domain scripting
- Next Thread: javascript syntax question
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxhelp animate array automatically beta box bug calendar cart checkbox class codes column cookies createrange() css cursor date debugger decimal design dom download dropdown editor element embed enter error explorer firefox focus frameworks getselection google gwt hint html htmlform ie7 iframe images index internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jsp jump listbox maps masterpage math menu microsoft mimic mp4 object onmouseover parent paypal php player position post problem programming progressbar prototype redirect regex runtime safari scale scriptlets search select session shopping size sql text textarea toggle variables w3c website window windowofwords windowsxp wysiwyg \n





