Current time: 12-11-2017, 11:04 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:
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:

Code:
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.

Code:
<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.

Code:
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
Find all posts by this user
Quote this message in a reply
11-21-2010, 03:58 AM
Post: #2
RE: Adsense IFrame Asynchronous Trick
(11-21-2010 12:16 AM)green-watch.org Wrote:  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.

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 </body>
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.

cdnplanet.com: We help you select the right CDN
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


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