Can't figure this out for some reason

Discussion in 'JavaScript' started by bigrollerdave, Feb 10, 2008.

  1. #1
    Below is a simple random banner rotator. for some reason when I run the code it's saying adsLink has no properties.

    javascript.js
    var rand_num = Math.floor(Math.random()*5);
    adsLink = window.document.getElementById('mylink');
    adsImage = window.document.getElementById('adImage');
    switch(rand_num){
    	case 1: { 
    		adsLink.href='/out.php?out=http://www.shockinghumor.com';
    		adsImage.src='http://i182.photobucket.com/albums/x7/campusbreak/ads/header.jpg';
    		adsImage.height='86';
    		adsImage.width='441';
    	}
    	case 2: { 
    		adsLink.href='/out.php?out=http://www.ebaumsworld.com/cgi-bin/top50/rankem.cgi?id=campus';
    		adsImage.src='http://i182.photobucket.com/albums/x7/campusbreak/ads/humorportal_04.gif';
    		adsImage.height='85';
    		adsImage.width='406';
    	}
    	case 3: { 
    		adsLink.href='/out.php?out=http://www.dumbie.com';
    		adsImage.src='http://i182.photobucket.com/albums/x7/campusbreak/ads/dumbie.png';
    		adsImage.height='133';
    		adsImage.width='460';
    	}
    	case 4: { 
    		adsLink.href='/out.php?out=http://www.zunked.com';
    		adsImage.src='http://i182.photobucket.com/albums/x7/campusbreak/ads/z_zunked.jpg';
    		adsImage.height='85';
    		adsImage.width='468';
    	}
    }
    Code (markup):
    here is code on my homepage

    <a href="" id="mylink" target="_blank"><img src="" id="adImage" name="adImage" border="0" width="468" height="60"></a>
    HTML:
    Any help would be much appreciated

    Thanks
     
    bigrollerdave, Feb 10, 2008 IP
  2. imvain2

    imvain2 Peon

    Messages:
    218
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The problem that I had when testing your code was the fact that I was putting the javascript before the img and link, which is why the browser was telling you it had no idea what you were asking for when referring to the link and img's element.

    I updated your code because the random function wasn't working for me, and to make additions easier if you have tons.

    
    <script>
    function RandomBanner(){
    
    var Banners = new Array();
    Banners[0] = new Array();
    Banners[0]["url"] = "http://i182.photobucket.com";
    Banners[0]["img"] = "http://i182.photobucket.com/albums/x7/campusbreak/ads/header.jpg";
    Banners[0]["height"] = "86";
    Banners[0]["width"] = "441";
    
    Banners[1] = new Array();
    Banners[1]["url"] = "/out.php?out=http://www.ebaumsworld.com/cgi-bin/top50/rankem.cgi?id=campus";
    Banners[1]["img"] = "http://i182.photobucket.com/albums/x7/campusbreak/ads/humorportal_04.gif";
    Banners[1]["height"] = "85";
    Banners[1]["width"] = "406";
    
    Banners[2] = new Array();
    Banners[2]["url"] = "/out.php?out=http://www.dumbie.com";
    Banners[2]["img"] = "http://i182.photobucket.com/albums/x7/campusbreak/ads/dumbie.png";
    Banners[2]["height"] = "133";
    Banners[2]["width"] = "460";
    
    Banners[3] = new Array();
    Banners[3]["url"] = "/out.php?out=http://www.zunked.com";
    Banners[3]["img"] = "http://i182.photobucket.com/albums/x7/campusbreak/ads/z_zunked.jpg";
    Banners[3]["height"] = "85";
    Banners[3]["width"] = "468";
    
    var rand_num = Math.floor(Math.random()*Banners.length);
    adsLink = window.document.getElementById('mylink');
    adsImage = window.document.getElementById('adImage');
    
    adsLink.href=Banners[rand_num]["url"];
    adsImage.src=Banners[rand_num]["img"];
    adsImage.height=Banners[rand_num]["height"];
    adsImage.width=Banners[rand_num]["width"];
    
    }
    window.onload = RandomBanner;
    </script>
    
    Code (markup):
     
    imvain2, Feb 10, 2008 IP
  3. imvain2

    imvain2 Peon

    Messages:
    218
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    the only real change that NEEDED to be made, was I made it into a function and ran the function window.onload.
     
    imvain2, Feb 10, 2008 IP