Show a div if only google ad is present

86Stang picture 86Stang · Aug 29, 2012 · Viewed 9.1k times · Source

Using javascript, I need to display a DIV but only if it contains a Google Ad. Here's the code I'm working with via Google:

<div id='div-gpt-ad-1346196436229-0'>
    <script type='text/javascript'>
        googletag.cmd.push(function() { googletag.display('div-gpt-ad-1346196436229-0'); });
    </script>
</div>

The DIV ID in the above code was generated by Google for the ad slot.

Any thoughts or directions I can get? Admittedly I'm pretty weak in the javascripting world so please be gentle. :)

Answer

Brandon H picture Brandon H · Jan 18, 2013

Just include this tag in your header code like so.

<script type='text/javascript'>
googletag.defineSlot('AdSlot-name', [AdSize], 
'div-gpt-ad-1346196436229-0').addService(googletag.pubads());
googletag.pubads().collapseEmptyDivs();
</script>

The tag you add is:

googletag.pubads().collapseEmptyDivs();