-6

I have a website that I want to rotate my affiliate links on a stream player that I have created. I have searched everywhere for a script but can not find one. I am not trying to use a service that does it for me. I want to script it myself. Any suggestions?

Retro617
  • 13
  • 1
  • 7
  • 1
    I suggest you start writing some code, and then ask a question when there is something specific to ask about. – 4castle Dec 15 '16 at 02:17
  • I totally understand where you are coming from, but that was the problem. I work mostly in C# but am building a site in html and CSS and need a banner rotator. Sorry if I offended. – Retro617 Dec 16 '16 at 03:37

2 Answers2

0

This is a great js plugin for a slider/carousel that will rotate between different slides (or banners in your case.)

http://kenwheeler.github.io/slick/

See the autoplay feature.

Hope it helps.

Brad
  • 8,044
  • 10
  • 39
  • 50
  • Looks like a great service. @Retro617, I'd highly recommend using this if you don't want to write your own and are looking for outside code anyway. – brld Dec 15 '16 at 02:38
  • 1
    Why the down vote? – Brad Dec 15 '16 at 02:40
  • Im not sure, I got one too. – brld Dec 15 '16 at 02:47
  • Not very nice, we are trying to help you mate, if its not the exact thing you are after thats fine, but no need down vote us – Brad Dec 15 '16 at 02:48
  • 1
    I agree. I spend time trying to find good resources to help. If you have feedback, please give it in a constructive and meaningful way. – brld Dec 15 '16 at 02:50
  • @brid I gave you an upvote mate :) – Brad Dec 15 '16 at 02:54
  • @brld I see you're trying to be helpful, but this question is off-topic for Stack Overflow. We don't want people asking questions where all you do is make a Google search for them, especially for product recommendations. – 4castle Dec 15 '16 at 03:58
  • then downvote the question not the answers – Brad Dec 15 '16 at 04:14
  • Thanks @Brad! Gave you one too! :) – brld Dec 16 '16 at 22:13
0

In the future, try to give this a shot for yourself and ask more specific questions. I don't see why you don't want to use a service if you're not writing it anyway.

However, since you are, here's my suggestion.

Javascript:

var banners = [
  {
    "img": "tnl-banner-steve-jobs-01.png",
    "url": "http://news.stanford.edu/news/2005/june15/jobs-061505.html",
    "weight": 1
  },
  {
    "img": "tnl-banner-steve-jobs-02.png",
    "url": "http://news.stanford.edu/news/2005/june15/jobs-061505.html",
    "weight": 1
  },
  {
    "img": "tnl-banner-steve-jobs-03.png",
    "url": "http://news.stanford.edu/news/2005/june15/jobs-061505.html",
    "weight": 1
  },
  {
    "img": "tnl-banner-steve-jobs-04.png",
    "url": "http://news.stanford.edu/news/2005/june15/jobs-061505.html",
    "weight": 1
  },
  {
    "img": "tnl-banner-steve-jobs-05.png",
    "url": "http://news.stanford.edu/news/2005/june15/jobs-061505.html",
    "weight": 4
  }
];

And here's the script to add into your HTML:

<script>

// Fetch the banner setup file.
var filename = getURLParameter("type")+".js";
jQuery.getScript(filename, function(){
  var banner = randomBanner();
  // Add the banner to the page body.
  $('body').append("<a target=\"tnl_ad\" href=\""+banner["url"]+"\">" +
    "<img src=\"banners/"+banner["img"]+"\"></a>");
})
  .fail(function(jqxhr, settings, exception) {
    console.log("Error parsing " + filename + ": " + exception.message);
  }
)

function randomBanner() {
    var totalWeight = 0, cummulativeWeight = 0, i;
    // Add up the weights.
    for (i = 0; i < banners.length; i++) {
        totalWeight += banners[i]["weight"];
    }
    console.log("Total weight: " + totalWeight);
    var random = Math.floor(Math.random() * totalWeight);
    // Find which bucket the random value is in.
    for (i = 0; i < banners.length; i++) {
        cummulativeWeight += banners[i]["weight"];
        if (random < cummulativeWeight) {
            return(banners[i]);
        }
    }
}

function getURLParameter(name){
  return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]);
}
</script>

Source

brld
  • 188
  • 2
  • 16
  • If the question wanted library/tutorial recommendations, it would have been off-topic because those questions are opinion-based. Please don't answer this question. It will only encourage the behavior. – 4castle Dec 15 '16 at 02:40
  • Thank you for your help and I apologize if I was vague. I work with some javascript but not enough to pull this off. I really appreciate your help. – Retro617 Dec 16 '16 at 03:38
  • Glad I could help. I'd recommend getting fluent with Javascript like this--it really is so important and can allow you to do things like this. – brld Dec 16 '16 at 22:12
  • For those landing here in 2021 or later, feel free to try out the npm package **[ad-rotator](https://www.npmjs.com/package/ad-rotator)** – Niket Pathak Aug 25 '21 at 22:30