Programming and Technology
RSS icon Home icon
  • Load Your Ads Last

    Posted on November 19th, 2009 brandon 1 comment

    Nobody likes ads but we can all agree they are a necessary component of doing business online. I recently had some experience dealing with adify, a aggregating ad platform which pipes in banners from a multitude of sources some with questionable practices in regards to loading performance. I had just launched a beatifully crafted website that employed all the tricks in the book for quick page loads and it was snappy… until we embedded the ad code. I like to load my images asynchronously and I like to execute javascript only once the document has finished loading so imagine the pain of patiently watching little animated gif spinners swirling away for as long as 6-30 seconds because the browser is choking on javascript from the banner ads in the middle of the page. Normally the solution would be to fix the javascript, but being a third party ad network, the embedded code was far beyond my control, so clearly this needed rethinking. I needed to figure out how to load the ads asynchronously as well.

    This sounds simple at first but after some googling the most common solution is to load the ad code into hidden iframes at the bottom of the page and then move the html nodes using javascript after the document has finished loading. Ick. I decided to employ some AJAX instead. Here’s how I made my ads load asynchronously:

    I start with some placeholder html.

    1.  
    2. <div class="advertisement" src="/ads/view?id=xxxx"></div>
    3.  

    This node must have the css class ‘advertisement’ and a src attribute specifying a url where the ad embed code can be retrieved from. In my project, built using the Zend Framework, I had a designated AdController class that served up particular ad code depending on which ad space I needed to fill.

    1.  
    2. jQuery(document).ready(function(){
    3.  
    4.    jQuery(‘.advertisement’).each(function(){
    5.       var adcont = jQuery(this);
    6.       var adurl = adcont.attr(’src’);
    7.       adcont.html(‘<iframe style="width: 100%; height: 100%; margin: 0; padding:0" scrolling="no" frameborder="0" allowtransparency="true" marginheight="0" marginwidth="0"  src="’ + adurl + ‘"></iframe>’);
    8.    });
    9. });
    10.  

    Next we need a little bit of jquery that scans the document for nodes with the css class ‘advertisement’. It expects every node it finds to also have the ’src’ attribute which it grabs and assigns to a dynamically generated iframe.

    Keep in mind this happens ‘after’ everything else has initialized that way your site remains responsive. Ok, so it’s not really AJAX but the effect is the same, the ads load last and they load asynchronously. You’re users will thank you.

    • Share/Bookmark
     

    One response to “Load Your Ads Last”

    1. But this way ads would not be able to access page contents, which is quite important for context ads.

    Leave a reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes