Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Script that randomizes the appearance of two or more scripts?

    Newbie here, so I hope this question is in the right section. I have been using the following script below to randomize in-house ad banners on my site. Those that are being randomly shown are just HREF image and destination links. However, I'm trying to figure out how I can randomize blocks of javascript in the places of the variables. Here's the original code with my site info obviously removed.

    Code:
    <br><script type="text/javascript">
    var images = [],
    index = 0;
    
    images[0] = "<a href = 'http://mysite.com/index.php?action=page;id=1'><img src='http://mysite.com/banner1.png'></a>";
    
    images[1] = "<a href = 'http://mysite.com/index.php?action=page;id=2'><img src='http://mysite.com/banner2.png'></a>";
    
    index = Math.floor(Math.random() * images.length);
    document.write(images[index]);
    </script>
    What I'm really want to do is insert the javascript ad coding instead of the image/link setup, but running into errors. I feel like it's either that I'm just a small proper parse away from having it work, but just not sure. I tried just replacing the code below with the <script></script> still housed in quotes, but it didn't work.

    Code:
    <a href = 'http://mysite.com/index.php?action=page;id=1'><img src='http://mysite.com/banner1.png'></a>
    Particularly, I'm trying to randomize some of the Amazon Affiliate widgets like the somewhat censored one below in place of what otherwise would be the code above.

    Code:
    <SCRIPT charset="utf-8" type="text/javascript" src="http://ws-na.amazon-adsystem.com/widgets/q?rt=tf_cw&ServiceVersion=XXXXXXXX&MarketPlace=US&ID=XXXXXXXXX%2FUS%2Fanarnati-20%2F8010%XXXXXXXXXXXXXX-b91c-0cc712bacc66&Operation=GetScriptTemplate"> </SCRIPT>
    Needless to say, I'm scratching my head over this. If any of you have any feedback, it would be greatly appreciated. Thanks in advance!

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Quote Originally Posted by Devistator View Post
    What I'm really want to do is insert the javascript ad coding instead of the image/link setup, but running into errors. !



    html file ...
    Code:
     
     <body>
     
     <script>
     rnd=Math.floor((Math.random()*3)+1);
    document.write("<script src='js",rnd,".js'></scr","ipt>")
      </script>     
      
    </body>
    js file js1.js ...

    Code:
     
     alert("1")
    js file js2.js ...

    Code:
     
     alert("2")
    js file js3.js ...

    Code:
     
     alert("3")
    Last edited by DaveyErwin; 12-25-2013 at 06:55 PM.

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,772
    Thanks
    23
    Thanked 550 Times in 549 Posts
    Merry Christams Devistator, Welcome to the forums.
    IMHO I's do this server side with PHP or something similar, but for you. Math.random() don't give zero as a return so images[0] won't work unless you subtract 1.
    And document.write() is only good if you have nothing else on the page.
    Try this for images:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Title of the document</title>
    </head>
    
    <body>
    <a href = 'http://mysite.com/index.php?action=page;id=1'><img src='http://mysite.com/banner1.png' id="first" style="display:none;"></a>
    <a href = 'http://mysite.com/index.php?action=page;id=2'><img src='http://mysite.com/banner2.png' id="second" style="display:none;"></a>
    
    <script type="text/javascript">
    	var images = [], index = 1;
    	images[1] = "first";
    	images[2] = "second";
    	index = Math.floor((Math.random() * 2)+1);
    	document.getElementById(images[index]).style.display = 'block';
    </script>
    </body>
    </html>
    Or you could use this for various array sizes:
    Code:
    var howmany = images.length-1;
    index = Math.floor((Math.random() * howmany)+1);
    Last edited by sunfighter; 12-25-2013 at 05:12 PM.
    Evolution - The non-random survival of random variants.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •