Adsense and Javascript

Discussion in 'JavaScript' started by twalters84, Apr 6, 2009.

  1. #1
    Greetings,

    I am trying to make a Google Adsense ad collapsible if there are no ads available. I need to know how to access the DIV above the adsense code and set the display to none. This is what I have tried below...

    Website Reference: Long Island Insurance - The ad to the right of the about us section usually is empty and needs to collapse.

    
    <cfif #SHOW_ADSENSE# EQ 1>
                        
        <div class="floatAdRight3">
    
        <script type="text/javascript"><!--
        google_ad_client = "MY_PUB_ID";
        /* BUSINESS ABOUT US */
        google_ad_slot = "AD_SLOT_ID";
        google_ad_width = 120;
        google_ad_height = 90;
        //-->
        </script>
                              
        <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
                                                
        </div>
                          
    </cfif>   
          
    
    Code (markup):
    Here is the javascript code that Google calls when no ads are available:

    
    var w = window;
    
    if (w.parent.document.getElementsByName) 
    {
        var google_ad_frames = w.parent.document.getElementsByName("google_ads_frame");
      
        for (var i = 0; i < google_ad_frames.length; i++) 
        {
            var f = google_ad_frames[i];
    
            if (f.contentDocument) 
            {
                if (w.document == f.contentDocument) 
                {
                    f.height = f.width = 0;
                    f.style.display="none";
                }
            } 
            else if (f.contentWindow) 
            {
                if (w == f.contentWindow) 
                {
                    f.height = f.width = 0;
                    f.style.display="none";
                }
            } 
        }
    }
    
    Code (markup):
    The ad space is not collapsing correctly. Any suggestions are most welcome. Is there a way to access the correct DIV without setting ID numbers?

    Sincerely,
    Travis Walters
     
    twalters84, Apr 6, 2009 IP