Need a very simple JS code ads rotation 50%/50% for AdSense Code

Discussion in 'JavaScript' started by donald, Feb 29, 2008.

  1. #1
    It's something very simple...

    I'm looking for a code that will display Ads 50% one code and 50% another.

    I find many similar JS scripts but they work on banners ads which in this case I'm looking to display AdSense code (2 deffrent ones)



    Does anyone know where can I find JS that does it ?
     
    donald, Feb 29, 2008 IP
  2. walkere

    walkere Active Member

    Messages:
    112
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Just tested this out, and it worked fine.

    In order to split it 50/50, you can use a random number. Math.random() gives you a random value from 0 to 1, so if Math.rand() is greater than .5, display one ad, otherwise display the other.

    Then, you conditionally use either set of AdSense ad_slot, width, and height settings.

    <script type="text/javascript"><!--
    google_ad_client = "pub-**************";
    if (Math.random() > .5) {
      google_ad_slot = "5396010225";
      google_ad_width = 250;
      google_ad_height = 250;
    } else {
      google_ad_slot = "3900536874";
      google_ad_width = 250;
      google_ad_height = 250;
    }
    //-->
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    HTML:
    Replace the ad_slot, ad_width, and ad_height values with the ones for your two different ad groups and it should work. Also make sure you insert your own publisher id.

    Good luck,
    - Walkere
     
    walkere, Feb 29, 2008 IP
  3. donald

    donald Active Member

    Messages:
    668
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thanks, Can I use it for 2 deffrent publisher IDs ?
     
    donald, Feb 29, 2008 IP
  4. walkere

    walkere Active Member

    Messages:
    112
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Should work fine.

    Just move the line...

    google_ad_client = "pub-**************";
    Code (markup):
    inside the conditional statement and you should be fine.

    - Walkere
     
    walkere, Feb 29, 2008 IP