javascript problem: what am I missing?

Discussion in 'Software' started by TheDoug, Apr 24, 2007.

  1. TheDoug

    TheDoug MajorGeek

    The basic goal here is to switch randomly between two banner ads sourced from individual javascripts, inserting them in the <p> tag in the body of the document. At this point I can't tell if the problem is with the code, or with the fact it just may not like calling the .js files. Any assistance would be most appreciated.

    Code:
    <html>
    <head>
    <script type="text/javascript">
          
                    window.onload = function() {
                            var adBlock = document.getElementById("adBlock");
                            var scriptTag = document.createElement("script");
                            scriptTag.type = "text/javascript";
                    
                            if (Math.round(Math.random()) == 0) {
                                    scriptTag.src = "http://www.my1domain.com/ad1.js";
                            } else {
                                    scriptTag.src = "http://www.my2domain.com/ad2.js";
                            }
                    
                            adBlock.appendChild(scriptTag);
                    }
            </script>
    
    </head>
    <body>
    <p id="adBlock" align=center></p>
    </body>
    </html>
     
  2. matt.chugg

    matt.chugg MajorGeek

    There are lots of different ways you can accomplish this.

    heres how I would do it

    Code:
    	banners = new Array(2);
    
    	banners[0] = "<img src='http://www.my1domain.com/ad1.js'>";
    	banners[1] = "<img src='http://www.my2domain.com/ad2.js'>";
    
    	index = Math.floor(Math.random() * banners.length);
    
    	document.write(banners[index]);
    
    or in less lines

    Code:
    	banners = new Array(2);
    
    	banners[0] = "<img src='http://www.my1domain.com/ad1.js'>";
    	banners[1] = "<img src='http://www.my2domain.com/ad2.js'>";
    
    	document.write(banners[Math.floor(Math.random() * banners.length)]);
    
    This way you can just add new banner lines and not have to change any numbers since the random function is between 0 and the size of the array.
     
  3. TheDoug

    TheDoug MajorGeek

    Excellent. Thanks. With only minor modification ('script src=' instead of 'img src='), and moving the document.write to the spot where I want the ad, it looks like it's behaving exactly as desired.

    Edit: Spoke too soon. Works in Moz/FF, not IE.
     
  4. TheDoug

    TheDoug MajorGeek

    Got it figured out:

    Code:
    var banners = new Array(2);
    
    banners[0] = "<script src='http://www.my1domain.com/ad1.js'><\/script>";
    banners[1] = "<script src='http://www.my2domain.com/ad2.js'><\/script>";
    
    index = Math.floor(Math.random() * banners.length);
    
    document.write(banners[index]);
    
    Needed closing <\/script> tags for IE to properly handle the called scripts. It's always fun when technically "broken" code works in some browsers.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds