How to use Random script

Asked

Viewed 100 times

1

I have 3 Google Adsense scripts with 300 x 250 - 160 x 600 - 728 x 90

How I do for each time the page is loaded 1 of these scripts is shown?

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

Example:

I own the one that is used for iframe, but in the case of Adsense I need to use the full 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 mo Randon

       <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>
  • And you want to avoid repeating one of them before you "turn around" or it can be repeated randomly?

  • Can be repeated, but other scripts are also shown

  • I put an answer. If you have code or urls of these scripts that you don’t know how to integrate put in the question.

  • I added an example to the answer.

  • I added the scripts I want to use, this example of iframe I showed I can’t use, google won’t let me, I need to use google’s own code on the page, I can’t use iframe

  • This football is out of this world huehuehuehue

Show 1 more comment

1 answer

2

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

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

To have only integers (to use as array input 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 is not enough to do this only in the browser, you have to have server logic.

An example would be 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]);
})();
  • This example works well with iframe, but I need to use the full script instead of iframe

  • @I joined the example with the new version, it was a small adjustment.

  • Can’t use the entire script? I can’t move the code I need to use the whole script without modifications

Browser other questions tagged

You are not signed in. Login or sign up in order to post.