Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random Header Link Issue

    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:

    Code:
    <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!

  • #2
    New Coder
    Join Date
    Feb 2010
    Posts
    44
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •