Randomize Which JS Is Called?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • incredibleworkethic
    Confirmed User
    • Sep 2009
    • 2302

    #1

    Tech Randomize Which JS Is Called?

    I just want to thank everyone in advance for the question I'm asking, especially if you're able to help me out here.

    Say I have three javascript files OR pieces of javascript I want to run. I don't care if I'm calling three files, or one with conditions.

    How do I randomize it so user lands on the website, the command is executed randomly.

    So

    mypopup.js
    advertiserpopup.js
    anotheroneofmypopups.js

    Is there a way to randomly call one of those?

    Or perhaps use it in

    allpopups.js = 3 codes, randomized per visit.

    The goal is to not run both a CPM and my own popup at the same time.

    Thank you!
  • Miguel T
    ♦ Web Developer ♦
    • May 2005
    • 12473

    #2
    Just don't with PHP
    Quicker and easier.

    Full Stack Webdeveloper: HTML5/CSS3, jQuery, AJAX, ElevatedX, NATS, MechBunny, Wordpress

    Comment

    • incredibleworkethic
      Confirmed User
      • Sep 2009
      • 2302

      #3
      Originally posted by Miguel T
      Just don't with PHP
      Quicker and easier.
      I modified this script (4 options, you can change to 3) and replace images with other JS

      It worked!

      But what is this php you speak of? :D I'm calling it in a wordpress theme. And the ads are in javascript to make the popup. Any ideas?



      var picnumber = Math.floor((Math.random() * 4) + 1);

      if (picnumber == 1){
      img.src = '1.jpg';
      } else if (picnumber == 2) {
      img.src = '2.jpg';
      } else if (picnumber == 3) {
      img.src = '3.jpg';
      } else {
      img.src = '4.jpg';
      }

      Comment

      • Colmike9
        (>^_^)b
        • Dec 2011
        • 7230

        #4
        I smoked a bit and I'm kind of rusty with this, but would it be something like this in PHP?:

        <?php

        $a=array("mypopup.js","advertiserpopup.js","anothe roneofmypopups.js");

        shuffle($a);

        echo '<script type="text/javascript" src="'("$a[0]")'"></script>';

        ?>

        I know that there's a better way to randomize by picking out a random number then using that for the array number to use rather than using Shuffle, but it's easier and I don't think there will be much difference in load with 3 variables.
        Join the BEST cam affiliate program on the internet!
        I've referred over $1.7mil in spending this past year, you should join in.
        I make a lot more money in the medical field in a lab now, fuck you guys. Don't ask me to come back, but do join Chaturbate in my sig, it still makes bank without me touching shit for years..

        Comment

        Working...