javascriptjqueryasynchronousadsenseopenx

Delay Ad Scripts Until All Other JS Has Loaded?


I'm using Revive Ad Server, and I moved the Invocation Code for my ads in divs below my footer, and then used jQuery to append them to where they're supposed to load. How can I delay the loading of the ads so they rest of my scripts can load? For example, I'm using Nivo Slider (which is loaded in head), but the first image won't display until all the ad scripts have loaded, which takes a couple seconds.

HTML with Invocation Code:

    <div class="hidden">
        <div id="leaderboard">
            <script type='text/javascript'><!--//<![CDATA[
               var m3_u = (location.protocol=='https:'?'https://www.website.com/adserver/www/delivery/ajs.php':'http://www.website.com/adserver/www/delivery/ajs.php');
               var m3_r = Math.floor(Math.random()*99999999999);
               if (!document.MAX_used) document.MAX_used = ',';
               document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
               document.write ("?zoneid=1&amp;blockcampaign=1");
               document.write ('&amp;cb=' + m3_r);
               if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
               document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));
               document.write ("&amp;loc=" + escape(window.location));
               if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
               if (document.context) document.write ("&context=" + escape(document.context));
               if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
               document.write ("'><\/scr"+"ipt>");
            //]]>--></script><noscript><a href='http://www.website.com/adserver/www/delivery/ck.php?n=a2ce62fe&amp;cb=INSERT_RANDOM_NUMBER_HERE' target='_blank'><img src='http://www.website.com/adserver/www/delivery/avw.php?zoneid=1&amp;cb=INSERT_RANDOM_NUMBER_HERE&amp;n=a2ce62fe' border='0' alt='' /></a></noscript>
        </div>
        <div id="wide-skyscraper">
            <script type='text/javascript'><!--//<![CDATA[
               var m3_u = (location.protocol=='https:'?'https://www.website.com/adserver/www/delivery/ajs.php':'http://www.website.com/adserver/www/delivery/ajs.php');
               var m3_r = Math.floor(Math.random()*99999999999);
               if (!document.MAX_used) document.MAX_used = ',';
               document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
               document.write ("?zoneid=2&amp;blockcampaign=1");
               document.write ('&amp;cb=' + m3_r);
               if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
               document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));
               document.write ("&amp;loc=" + escape(window.location));
               if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
               if (document.context) document.write ("&context=" + escape(document.context));
               if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
               document.write ("'><\/scr"+"ipt>");
            //]]>--></script><noscript><a href='http://www.website.com/adserver/www/delivery/ck.php?n=a16afa56&amp;cb=INSERT_RANDOM_NUMBER_HERE' target='_blank'><img src='http://www.website.com/adserver/www/delivery/avw.php?zoneid=2&amp;cb=INSERT_RANDOM_NUMBER_HERE&amp;n=a16afa56' border='0' alt='' /></a></noscript>
        </div>
    </div>

jQuery:

    $(document).ready(function () {

        // Nivo Slider code

    });

    $(window).load(function() {

        $('#leaderboard').contents().appendTo($('.leaderboard'));
        $('#wide-skyscraper').contents().appendTo($('.wide-skyscraper'));

    });

Solution

  • Scripts

        element.write.js
        writeCapture2.js
    

    JavaScript

        $(document).ready(function () {
            $.getScript('/assets/js/plugins/writeCapture.js', function() {
                if ($('#leaderboard-1').length) {
                    writeCapture.write(
                        document.getElementById('leaderboard-1'),
                        '<scr' + 'ipt>' +
                            'var revSrc = (document.location.protocol === "https:" ? "https:" : "http:")' +
                            '+ "//www.website.com/adserver/www/delivery/ajs.php"' +
                            '+ "?zoneid=1&blockcampaign=1&cb=" + Math.floor(Math.random()*99999999999)' +
                            '+ "&charset=UTF-8&loc=" + encodeURIComponent(window.location)' +
                            '+ (document.MAX_used !== "," ? ("&exclude=" + document.MAX_used) : "")' +
                            '+ (document.referrer ? ("&referer=" + encodeURIComponent(document.referrer)) : "")' +
                            '+ (document.context ? ("&context=" + encodeURIComponent(document.context)) : "")' +
                            '+ (document.mmm_fo ? "&mmm_fo=1" : "");' +
                            'document.write("<scr" +  "ipt src=\'" + revSrc + "\'></scr" + "ipt>");' +
                        '</scr' + 'ipt>'
                    );
                }
                if ($('#wide-skyscraper-1').length) {
                    writeCapture.write(
                        document.getElementById('wide-skyscraper-1'),
                        '<scr' + 'ipt>' +
                            'var revSrc = (document.location.protocol === "https:" ? "https:" : "http:")' +
                            '+ "//www.website.com/adserver/www/delivery/ajs.php"' +
                            '+ "?zoneid=2&blockcampaign=1&cb=" + Math.floor(Math.random()*99999999999)' +
                            '+ "&charset=UTF-8&loc=" + encodeURIComponent(window.location)' +
                            '+ (document.MAX_used !== "," ? ("&exclude=" + document.MAX_used) : "")' +
                            '+ (document.referrer ? ("&referer=" + encodeURIComponent(document.referrer)) : "")' +
                            '+ (document.context ? ("&context=" + encodeURIComponent(document.context)) : "")' +
                            '+ (document.mmm_fo ? "&mmm_fo=1" : "");' +
                            'document.write("<scr" +  "ipt src=\'" + revSrc + "\'></scr" + "ipt>");' +
                        '</scr' + 'ipt>'
                    );
                }
            });
        });