Title: Randomize Scripts in a Widget? :D
Last modified: August 21, 2016

---

# Randomize Scripts in a Widget? :D

 *  Resolved [johnsontk10](https://wordpress.org/support/users/johnsontk10/)
 * (@johnsontk10)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/randomize-scripts-in-a-widget-d/)
 * 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)

 *  [CrouchingBruin](https://wordpress.org/support/users/crouchingbruin/)
 * (@crouchingbruin)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/randomize-scripts-in-a-widget-d/#post-3956077)
 * 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](https://wordpress.org/support/users/johnsontk10/)
 * (@johnsontk10)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/randomize-scripts-in-a-widget-d/#post-3956133)
 * 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?
 *  [CrouchingBruin](https://wordpress.org/support/users/crouchingbruin/)
 * (@crouchingbruin)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/randomize-scripts-in-a-widget-d/#post-3956140)
 * 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](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](https://wordpress.org/support/users/johnsontk10/)
 * (@johnsontk10)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/randomize-scripts-in-a-widget-d/#post-3956141)
 * 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.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [johnsontk10](https://wordpress.org/support/users/johnsontk10/)
 * Last activity: [12 years, 9 months ago](https://wordpress.org/support/topic/randomize-scripts-in-a-widget-d/#post-3956141)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
