延迟广告脚本直到所有其他JS加载?

时间:2014-07-22 21:38:42

标签: javascript jquery asynchronous adsense openx

我正在使用Revive Ad Server,我在我的页脚下面的div中移动了我的广告的调用代码,然后使用jQuery将它们附加到他们应该加载的位置。如何延迟加载广告,以便我的其余脚本可以加载?例如,我使用Nivo Slider(以头部加载),但第一张图片不会显示,直到所有广告脚本都已加载,这需要几秒钟。

带调用代码的HTML:

    <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'));

    });

2 个答案:

答案 0 :(得分:2)

我无法说出您的ToS,但我认为可以通过在滑块加载后动态创建广告脚本而不是HTML中的硬编码来实现。广告网站可以对此进行检查,如果有,则无法正常工作;但是我会尝试以下方法:

HTML

<div class="leaderboard">
    <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 class="wide-skyscraper">
    <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>

的JavaScript

function doAds() {
    getAd('a2ce62fe', 1, '.leaderboard');
    getAd('a16afa56', 2, '.wide-skyscraper');
}
function getAd(n, zoneid, target) {
    var adScript = "<script type='text/javascript'><!-" + "-//<![CDATA[\n";
    adScript += "var m3_u = (location.protocol=='https:'?'https://www.website.com/adserver/www/delivery/ajs.php':'http://www.website.com/adserver/www/delivery/ajs.php');\n";
    adScript += "var m3_r = Math.floor(Math.random()*99999999999);\n"
    adScript += "if (!document.MAX_used) document.MAX_used = ',';\n";
    adScript += "document.write (\"<scr\"+\"ipt type='text/javascript' src='\"+m3_u);\n";
    adScript += "document.write (\"?zoneid=" + zoneid + "&amp;blockcampaign=1\");\n";
    adScript += "document.write ('&amp;cb=' + m3_r);\n"
    adScript += "if (document.MAX_used != ',') document.write (\"&amp;exclude=\" + document.MAX_used);\n";
    adScript += "document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));\n";
    adScript += "document.write (\"&amp;loc=\" + escape(window.location));\n"
    adScript += "if (document.referrer) document.write (\"&amp;referer=\" + escape(document.referrer));\n";
    adScript += "if (document.context) document.write (\"&context=\" + escape(document.context));\n";
    adScript += "if (document.mmm_fo) document.write (\"&amp;mmm_fo=1\"); document.write (\"'><\/scr\"+\"ipt>\");\n//]]>-" + "-></scr" + "ipt>";
    $(target).prepend(adScript);
}
$(document).ready(function() {
    // Nivo Slider code
    $('#slider').nivoSlider({
        afterLoad: function(){
            doAds();
        }   
    });
});

答案 1 :(得分:0)

脚本

    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>'
                );
            }
        });
    });