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

Changing href with javascript

Status
Not open for further replies.

reidgivens

Technical User
Mar 24, 2003
24
0
0
US
I was hoping someone could tell me why this doesn't work. It is a banner rotater.


----
<html>
<head><title>whats up</title>
<script LANGUAGE='JAVASCRIPT'>
<!-- Hide form old browsers
var banners = new Array(" var links = new Array(" var bnrCntr = 2;
function bancycle () {
bnrCntr = bnrCntr + 1;
if (bnrCntr == 3){
bnrCntr = 0;
}
document.banner.src = banners[bnrCntr];
document.link.href = links[bnrCntr];
setTimeout("bancycle()",3000);
}
//-->
</SCRIPT>
</head>
<body onload="setTimeout('bancycle()',3000)">
<a href=" name="link"><img src=" name="banner"></a>
</body>
</html>

----

any ideas?

Reid Givens
Webdeveloper
 
Hey I messed around with it for a while and couldnt figure anything out till I changed the link to an ID instead of a name and that worked just fine. I also changed you if statement incase you ever add more. And just incase you didnt know arrays start at 0. SO the first entry in the array is equal to 0 not one.
Hope that helps a little. Heres the code I ended up with.
Code:
<html>
<head><title>Not To Much</title>
<script LANGUAGE='JAVASCRIPT'>
<!-- Hide form old browsers
    var banners = new Array("[URL unfurl="true"]http://www.reidgivens.com/images/old_logo.gif","http://reidgivens.com/clir/web/site/images/logo_AD...","http://www.kfma.com/GRAPHICS/gifs/revenge185.gif");[/URL]
    var links = new Array("[URL unfurl="true"]http://www.reidgivens.com","http://www.reidgivens.com","http://www.kfma.com");[/URL]
    var bnrCntr = 0;
    function bancycle () {
        bnrCntr++
        if (bnrCntr>=banners.length){
        bnrCntr = 0;
        }
    document.banner.src = banners[bnrCntr];
  document.getElementById('linker').href = links[bnrCntr];
    setTimeout("bancycle()",3000);
status=bnrCntr;
}
//-->
</SCRIPT>
</head>
<body onload="setTimeout('bancycle()',3000)">
<a href="[URL unfurl="true"]http://www.kfma.com"[/URL] id="linker">

<img src="[URL unfurl="true"]http://www.kfma.com/GRAPHICS/gifs/revenge185.gif"[/URL] name="banner">

</a>
</body>
</html>
Matt
 
You'll need to declare your var bnrCntr outside of your function.

Put it right before the function.

The way you have it, you're always re-declaring your variable, and always setting it back to 2.

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
I'm an idiot. Ignore me.

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
Don't ignore this, though:

Code:
<html>
<head><title>whats up</title>
<script LANGUAGE='JAVASCRIPT'>
<!-- Hide form old browsers
var banners = new Array("[URL unfurl="true"]http://www.reidgivens.com/images/old_logo.gif","http://reidgivens.com/clir/web/site/images/logo_AD...","http://www.kfma.com/GRAPHICS/gifs/revenge185.gif");[/URL]
var links = new Array("[URL unfurl="true"]http://www.reidgivens.com","http://www.reidgivens.com","http://www.kfma.com");[/URL]

var bnrCntr = 2;
function bancycle() {
    alert(bnrCntr);
    bnrCntr = bnrCntr + 1;
    if (bnrCntr == 3)
        bnrCntr = 0;
	
	document.getElementById('banner').src = banners[bnrCntr];
	document.getElementById('linkstuff').href = links[bnrCntr];
    setTimeout("bancycle()",3000);
}
//-->
</SCRIPT>
</head>
<body onload="setTimeout('bancycle()',3000)">
<a href="[URL unfurl="true"]http://www.kfma.com"[/URL] id="linkstuff"><img src="[URL unfurl="true"]http://www.kfma.com/GRAPHICS/gifs/revenge185.gif"[/URL] id="banner"></a>
</body>
</html>

Avoid using reserver words like LINK. Also, use ID instead of NAME.

This one worked, although your first banner shows as an X.

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
(and remove the alert I put in there for debugging...)

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top