1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Javascript ads rotate

Discussion in 'JavaScript' started by Freeman93, Sep 23, 2016.

  1. #1
    Hello,
    Have 2 or more javascript banner ads from different ad networks.
    How to make these banners to rotate on each page refresh?
    I would appreciate if anyone who knows, is willing to share the knowledge :)
     
    Freeman93, Sep 23, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    I'm assuming you mean that you load the ads from one ad network on load 1, and then the other network on refresh, and so on and so forth?

    Just do it in the backend, and load the different javascript code on each page-load?
     
    PoPSiCLe, Sep 23, 2016 IP
  3. Freeman93

    Freeman93 Well-Known Member

    Messages:
    139
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #3
    Yes, that is what i mean. Each page refresh to load different javascript code.. and to be a loop. never stop loading the scripts.
     
    Freeman93, Sep 25, 2016 IP
  4. wordplucker

    wordplucker Well-Known Member

    Messages:
    205
    Likes Received:
    38
    Best Answers:
    1
    Trophy Points:
    105
    #4
    Why are you using javascript over PHP?

    What is the platform your site is running on? A CMS, like wordpress, or basic HTML?
     
    wordplucker, Sep 25, 2016 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Then you code it in the backend - fairly easy to do, and you could incorporate any number of ad networks. You could even make it so that it never loads the same network twice in a row (true random would make the same networks sometimes load again), and so forth. Why use javascript for this?
     
    PoPSiCLe, Sep 25, 2016 IP
  6. Freeman93

    Freeman93 Well-Known Member

    Messages:
    139
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #6
    Because my CMS doesn't allow using php in the template.
    If it doesn't take you much time, could please explain how to do this in both javascript and php?
     
    Freeman93, Sep 25, 2016 IP
  7. jermainluong

    jermainluong Greenhorn

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #7
    So your problem is determining when to load the ads 1, when to load the ads 2, right?
    You can do this in JavaScript by using localStorage
    function onload() {
         var lastAds = localStorage.getItem("lastAdsKey") || 1;
         if (lastAds == 1) {
              lastAds = 2;
              //TODO: load ads 2
         } else {
              lastAds = 1;
              //TODO: oad ads 1
         }
         localStorage.setItem("lastAdsKey", lastAds);
    }
    
    Code (JavaScript):
     
    jermainluong, Oct 6, 2016 IP