Adsense IFrame Asynchronous Trick

Hey There,

I wrote a thread awhile back wondering when Google AdSense would become asynchronous. For me, a big concern is getting that document complete time as early as possible because I have some stuff that happens on the onload event. AdSense was stopping this event from occurring early so I took AdSense off my website for awhile.

I came across this article on the internet:
http://geoland.org/2007/01/adsense-in-iframe/

With AdSense, content must be relevant to the page it is on. With IFrames, this is not possible unless you have a piece of code above the AdSense, which makes AdSense look outside of the IFrame:

document=parent.document;

On the website I found, the guy that wrote the code got in touch with the AdSense team and verified the legality of the code according to their terms of service.

So now we have an IFrame that loads AdSense code. This IFrame can easily be loaded on the body onload event.

<iframe id="adsenseFrame1" src="" height="1" width="145" frameborder="0" scrolling="no"></iframe>

Notice in the code above how the SRC attribute is an empty string.

function myBodyOnLoadEvent()
{
   resizeProductImages();
   importGoogleMapJS();
   showRightSideAds();	   		
}

function showRightSideAds()
{
   var mainContentRightLeft = document.getElementById('main-l');
   var mainContentRightRight = document.getElementById('main-r');
   var myGoogleAd = document.getElementById('GOOGLE_ADSENSE_RIGHT');
   var mainRightLeftHeight = mainContentRightLeft.offsetHeight;
   var mainRightRightHeight = mainContentRightRight.offsetHeight;			            
   var ADSpace = mainRightLeftHeight - mainRightRightHeight;

   if (ADSpace > 630)
   { 
      var adsenseFrame1 = document.getElementById('adsenseFrame1');
      adsenseFrame1.height = 625;
      adsenseFrame1.src = 'http://www.example.com/ADS/adsense.cfm?myAdID=1';	 
      myGoogleAd.style.display = 'block';
      myGoogleAd.style.marginBottom = '20px';
      ADSpace = ADSpace - 700;
   }			  
}		

When the body onload event occurs, it calls the showRightSideAds() function, which first checks to see if there is enough space to display the ad, and then if there is sets the IFrame SRC attribute, which loads the AdSense code at that point.

This solves the problem of AdSense slowing down the website. The body onload event occurs early and all my scripts execute very fast now. Please let me know what you think of my solution I found and expanded upon.

Sincerely,
Travis Walters

If i were doing this, I would get a confirmation directly from a google rep if this is ok or not…

Unlike this method which basically has same effect as source ordered content, what you are doing is delaying adsense until onload event…

Alternately what you could do is:-

  1. use aaron’s method to put adsense just before
  2. instead of attaching your logic to onload, attach it to a custom trigger which fires just before starting to load adsense…

Personally i messed with iframes with adsense years ago (regular iframe not dynamic one) and saw my eCPM go down… dont know if thats still the case… but something you should consider.