-1

I want to change the image and anchor link on refresh for my banners. I have 4 different banner images and 4 different links for each banner. So I need when user refresh the page each banner will load with own URL.

HTML code :

<div class="ad_body">
    <a href="http://www.domain.com/" target="_blank">
        <img src="images/banner/partner1.jpg">
    </a>
</div>

Thanks for any help.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101

1 Answers1

1

You could add images sources you have in array then choose from this array randomly :

$(function(){
    var links = ["link1", "link2", "link3"];
    var arr = ["partner1.jpg", "partner2.jpg", "partner3.jpg"];
    var random = Math.floor(Math.random() * arr.length) + 0;

    $(".ad_body a").attr("href", links[random]);
    $(".ad_body img").attr("src", "images/banner/"+arr[random]);
});

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101