• Resolved johnsontk10

    (@johnsontk10)


    Sup guys?

    I have a tiny javascript code that sits in a widget that shows an ad.

    It’s a tiny one line script.

    I have like 10 of the scripts, but only one can show up really.

    I would like therefore randomize the scripts.

    What’s the best way to accomplish that?

    Thanks for any ideas

Viewing 4 replies - 1 through 4 (of 4 total)
  • You should be able to use jQuery/JavaScript to randomly pick which one to display:

    <script>
    jQuery(document).ready(function($){
       var iRand = Math.floor(Math.random() * 10);
       switch(iRand)
       {
       case 0:
         // First ad code
         break;
       case 1:
         // Second ad code
         break;
    ...
       case 8:
         // 9th ad code
         break;
       default:
         // 10th ad code
       }
    }
    </script>

    Thread Starter johnsontk10

    (@johnsontk10)

    Thank you for your time boss 🙂

    I setup the code like this:

    <script>
    jQuery(document).ready(function($){
       var iRand = Math.floor(Math.random() * 5);
       switch(iRand)
       {
       case 0:
    script1
         break;
       case 1:
    script2
         break;
       case 2:
    script3
         break;
       case 3:
    script4
         break;
       default:
    script5
       }
    }
    </script>

    But it shows all 5 instead of picking one

    Any advice?

    OK, when you said you had one-line of javascript code for each ad, that’s not what you are actually using. You’re trying to embed a link to an external script, and you can’t embed <script> … </script> tags inside another set of <script> … </script> tags. So what you need to do is go inside each of the individual scripts (click on the link for it), then copy & paste the entire code for that script under each case.

    For example, the first script is at http://forms.aweber.com/form/79/620539579.js. Click on that link and you’ll see the actual code appear in your browser (it’s long). Copy & paste that code into where you have script1 in your above example. Do the same for the other scripts.

    Thread Starter johnsontk10

    (@johnsontk10)

    You sir are a genius 😀

    Thanks for your time, truly

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Randomize Scripts in a Widget? :D’ is closed to new replies.