MyBB Internal: One or more warnings occured. Please contact your administrator for assistance.
Adsense IFrame Asynchronous Trick
Current time: 10-28-2020, 10:45 PM Hello There, Guest! (LoginRegister)

Post Reply 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adsense IFrame Asynchronous Trick
11-21-2010, 12:16 AM
Post: #1
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:

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:


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()

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 = '';  = 'block'; = '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.

Travis Walters
Find all posts by this user
Quote this message in a reply
Post Reply 

Messages In This Thread
Adsense IFrame Asynchronous Trick - - 11-21-2010 12:16 AM

Forum Jump:

User(s) browsing this thread: 2 Guest(s)