How to use random script

1

I have 3 Google adsense scripts with 300x250 - 160x600 - 728x90

How do I get one of these scripts to be loaded each time the page is loaded?

I would like each Reload of the page to be shown 1 script.

Example:

I have the one that is used for iframe, but in the case of adsense I need to use the complete script.

    <script language="JavaScript">
    var quotes=new Array()
     quotes[0]='<iframe frameborder="0" height="250" name="videoplayer" 
    scrolling="no" src="http://"style="border: 0px #FFFFFF none;" width="300">
   </iframe>'
      quotes[1]='<iframe frameborder="0" height="250" name="videoplayer" 
      scrolling="no" src="http://"style="border: 0px #FFFFFF none;" width="300"></iframe>'
      quotes[2]='<iframe frameborder="0" height="250" name="videoplayer" 
      scrolling="no" src="http://"style="border: 0px #FFFFFF none;" width="300"></iframe>'
      var whichquote=Math.floor(Math.random()*(quotes.length))
      document.write(quotes[whichquote])
      </script>

Blocks I want to use in Randon mo

       <script async 
       src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">
        </script>
         <!-- lateral -->
         <ins class="adsbygoogle"
     style="display:inline-block;width:300px;height:250px"
     data-ad-client="ca-pub-2068602933738629"
      data-ad-slot="8173528505"></ins>
            <script>
          (adsbygoogle = window.adsbygoogle || []).push({});
         </script>

         <script async 
       src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">
        </script>  <ins
         class="adsbygoogle"
          style="display:inline-block;width:160px;height:600px"
            data-ad-client="ca-pub-2068602933738629"
             data-ad-slot="2353050305"></ins> <script>(adsbygoogle = 
         window.adsbygoogle || []).push({});</script>

              <script async 

          src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">
               </script>
           <!-- celular -->
           <ins class="adsbygoogle"
           style="display:inline-block;width:320px;height:100px"
           data-ad-client="ca-pub-2068602933738629"
          data-ad-slot="6337832563"></ins>
            <script>
           (adsbygoogle = window.adsbygoogle || []).push({});
            </script>
    
asked by anonymous 05.05.2017 / 18:12

1 answer

2

Math.random() gives you a decimal number between 0 and 1 .

To have a number between 0 and 2 you can Math.random() * 2 , that is a maximum of 2 is a minimum of 0.

To have only integers (to use as array index for example) you can use Math.round() .

function gerarNumero(){
    return Math.round(Math.random() * 2);
}

If you want to ensure that a number is only drawn once every iteration of the other numbers, you can not do this in the browser, you have to have logic on the server.

An example would look like this:

(function() {
  var script = document.createElement('script');
  script.src = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
  document.body.appendChild(script);
  (adsbygoogle = window.adsbygoogle || []).push({});

  function gerarNumero(max) {
    return Math.round(Math.random() * max);
  }
  var quotes = [
    '<ins class="adsbygoogle" style="display:inline-block;width:160px;height:600px" data-ad-client="ca-pub-2068602933738629" data-ad-slot="2353050305"></ins>',
    '<ins class="adsbygoogle" style="display:inline-block;width:300px;height:250px" data-ad-client="ca-pub-2068602933738629" data-ad-slot="8173528505"></ins>',
    '<ins class="adsbygoogle" style="display:inline-block;width:320px;height:100px" data-ad-client="ca-pub-2068602933738629" data-ad-slot="6337832563"></ins>'
  ];
  var index = gerarNumero(quotes.length - 1);
  var adDiv = document.createElement('div');
  adDiv.innerHTML = quotes[index]
  document.body.appendChild(adDiv.children[0]);
})();
    
05.05.2017 / 18:20