Load ads defered (without blocking)

Load ads defered (without blocking)
Ads’s scripts are evil, most part of them use document.write to create a new script element, and then it load an other script which make an iframe which load other script which make….and finally your page were blocked, not rendering, until ads are loaded.
We solved this problem using a very stupid defer technic. Steps:
make an empty container where ad has to be showed, ej:  <div id=”target”></div>
at bottom page, make second hide container, and put there the ad’s script, ej:  <div id=”src” style=”display:none;”> <script> relocateADS(’target’,’src’); //adsense ej </script> </div>
put relocateADS declaration before src div
relocateAds
This function is very simple, It just show the src div, and set the absolute position of  target to src. This script use jquery, but it is easy to change if you are not loading jquery
The final result: your page will load all content before ads.
:)

Ads’s scripts are evil, most part of them use document.write to create a new script element, and then it load an other script which make an iframe which load other script which make….and finally your page were blocked, not rendering, until ads are loaded.

We solved this problem using a very stupid defer technic. Steps:

  1. make an empty container where ad has to be showed, ej:  <div id=”target”></div>
  2. at bottom page, make second hide container, and put there the ad’s script, ej:  <div id=”src” style=”display:none;”> <script> relocateADS(’target’,’src’); //adsense ej </script> </div>
  3. put relocateADS declaration before src div

relocateAds

This function is very simple, It just show the src div, and set the absolute position of  target to src. This script use jquery, but it is easy to change if you are not loading jquery


/**
 * move all ads dom element from src div to target div now use absolute position
 * to iframe banners
 */
function relocateAds(tgt,src) {
    try{
        //get elements
        var $s = $('#' + src);
        var $t = $('#' + tgt);
        //vars
        var last_pos = 0;
        var last_height = 0;

        //set initial pos and show
        $s.css({position:'absolute'}).show();                   

        /**
         * set interval to check window/components resize
         * on pos change or height change reset
         */
        setInterval(function(){
            var pos = $t.offset();
            if(pos !== last_pos ){
                last_pos = pos;
                $s.css(pos);
            }
            var height = $s.height();
            if(height !== last_height){
                last_height = height;
                $t.css({height:last_height});
            }
        },1000);                

    }catch(e){}

}

The final result: your page will load all content before ads.

:)

This entry was posted in performance and tagged , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Matias
    Posted 2010/06/09 at 10:56 | Permalink

    Muy interesante lo voy a probar. Tengo miedo que tener este tipo de trucos multiplicados en cantidad para diversos aspectos de la pagina hagan que sume segundos en el tiempo de carga.
    Mas alla de eso, yo personalmente evitaria usar cualquier publicidad basada en i-frames.

    Pregunta.. hasta que punto se puede utilizar el mismo mecanismo para widgets de diversos tipos como los de clima por ejemplo.

    Saludos.

  2. admin
    Posted 2010/06/10 at 10:12 | Permalink

    @Matias: esta técnica es puntualmente para todo script de terceros donde no sabes que viene o como impacta, o sea, cuando traen sentencias de document.write que genera iframes o asi. Tipicamente Adsense. Pero se puede usar con cualquier tipo de widget. Eso si, esto no suma tiempo a la carga total de la pagina, ni lo mejora. Sino que mejora la percepción del usuario de carga.

    Suerte

  3. Posted 2010/10/05 at 06:35 | Permalink

    El código de la función genera errores fatales. Primero la definción de las variables de la función. Luego el bloque sobre obtener elementos. Ése ya no logré solucionarlo.

    La idea suena excelente. Sigo investigando para solucionar el posible error.

    Gracias por compartir.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>