...

View Full Version : Random Header Link Issue



matthiasroberts
06-13-2010, 11:18 PM
Hi,

I am working on a website that uses a random header. I have been trying to figure out how to adjust the code so that the header also serves as a clickable link. Here is the code that I have:




<script type="text/javascript">
var banner = new Array ()

banner[0]="http://lh4.ggpht.com/_KKbXY

banner[1]="http://lh5.ggpht.com/_KKbXY

banner[2]="http://lh3.ggpht.com/_KKbXY

banner[3]="http://lh3.ggpht.com/_KKbXY

banner[4]="http://lh5.ggpht.com/_KKbXY

banner[5]="http://lh3.ggpht.com/_KKbXY

banner[6]="http://lh3.ggpht.com/_KKbXY

banner[7]="http://lh3.ggpht.com/_KKbXY

banner[8]="http://lh6.ggpht.com/_KKbXY

var random=Math.floor(9*Math.random());
document.write("<style>");
document.write("#header{");
document.write(' background:url("'+ banner [random] +'") no-repeat top left;');
document.write("}");
document.write("</style>");


</script>


What do I need to add in order for the header to also serve as a link? I am completely new to JavaScript and I don't understand it very well. Thank you so much for your help!

Skychan
06-14-2010, 06:07 AM
In your html have an anchor or a div with an onclick event:
<a id="myanchor"></a>

In your script, right after your line:
var random=...
//do this instead
var anchor = document.getElementeById('myAnchor');
anchor.style.background = 'url("'+ banner [random] +'") no-repeat top left';
anchor.src = banner[random];

And that should do it, unless I made a syntax mistake, I haven't tested my code. You could use a div element and set the onclick or onmouseup event to run code like "document.location = banner[random]" instead. Or just stick with the anchor. Whatever works for you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum