Results 1 to 7 of 7

Thread: changing img's href dynamically

  1. #1

    Thread Starter
    Addicted Member TheGoldenShogun's Avatar
    Join Date
    Mar 2001
    Location
    VA/MD... anywhere around the beltway
    Posts
    236

    changing img's href dynamically

    I'm trying to make a dynamic banner generator. Got the images to rotate randomly on a timer (every 10 seconds a new banner pops up) but I also need to change the href of that image to point to a different url every ten seconds. Here's a look at my code... onload calls setBanner and I got some PHP in there.


    function setBanner()
    {
    randomNumber = Math.floor(Math.random()*<?echo($numberBanners);?>);
    document.mybanner.src=''+bannerz[4]+'';

    setTimeout("pickBanner()", 8000);
    }

    function pickBanner()
    {
    randomNumber = Math.floor(Math.random()*<?echo($numberBanners);?>);
    document.mybanner.src=''+bannerz[randomNumber]+'';
    //document.mybanner.href=''+urls[randomNumber]+'';

    window.status = "Banner is now ... "+bannerz[randomNumber];
    setTimeout("pickBanner()", 8000);
    }

  2. #2
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    Make an onClick command to the image that calls a function like so:

    <a href="#" onClick="showURL();"><img src="banner is here"></a>

    then in that function check to see which banner element is showing from its array and so the appropriate link that is stored in a different array.

    Say banner[4] is showing. So inside your showURL() you would have:

    window.open(link[4]);

    -Matt
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  3. #3

    Thread Starter
    Addicted Member TheGoldenShogun's Avatar
    Join Date
    Mar 2001
    Location
    VA/MD... anywhere around the beltway
    Posts
    236
    gotcha, I had a brain freeze on that... thanks cpradio

  4. #4
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    I think you could also just give your href an id/name and change it's href the same way you changed your image's src.

  5. #5
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    That is a nice idea but I beleive some browsers will not accept that. (For example Netscape)
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  6. #6
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    another possiblity..

    I dont know if you can do this with php..

    but using asp..I was able to change the content type to an image, and code the asp page to output an image to the Reponse stream..then when you goto the page whatever.asp, it becomes an image, so you could dymacilly have change by coding it in the asp page then in the img tage use

    src="yourpage.asp"


    again I dont know how in php if it is possible.


    just some FYI
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  7. #7
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    fdor example..see my banner in my sig..if you notice..the text changes when you refresh the page..and if you were to look at the link..you would see a .aspx extension(ASP .NET)..and it uses the technique i described.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

Posting Permissions

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



Click Here to Expand Forum to Full Width