代码之家  ›  专栏  ›  技术社区  ›  pjmorse

如何防止OpenX阻塞页面加载?

  •  9
  • pjmorse  · 技术社区  · 15 年前

    我们用的是OpenX的 single page call ,我们在CSS中给div显式的大小,这样它们就可以在没有内容的情况下进行布局,但仍然加载脚本阻止页面加载。使用OpenX加速页面还有其他最佳实践吗?

    5 回复  |  直到 13 年前
        1
  •  1
  •   Kevin    12 年前

    OpenX提供了一些关于如何异步加载标记的文档: http://docs.openx.com/ad_server/adtagguide_synchjs_implementing_async.html

    这需要一些手动调整他们的广告代码。他们的广告标签应该如何结束的例子:

    <html>
    <head></head>
    
    <body>
    
    Some content here.
    
    Ad goes here.
    
    <!-- Preserve space while the rest of the page loads. -->
    
    <div id="placeholderId" style="width:728px;height:90px">
    
    <!-- Fallback mechanism to use if unable to load the script tag. -->
    
    <noscript>
    <iframe id="4cb4e94bd5bb6" name="4cb4e94bd5bb6"
     src="http://d.example.com/w/1.0/afr?auid=8&target=
    _blank&cb=INSERT_RANDOM_NUMBER_HERE"
     frameborder="0" scrolling="no" width="728"
     height="90">
    <a href="http://d.example.com/w/1.0/rc?cs=
    4cb4e94bd5bb6&cb=INSERT_RANDOM_NUMBER_HERE"
     target="_blank">
    <img src="http://d.example.com/w/1.0/ai?auid=8&cs=
    4cb4e94bd5bb6&cb=INSERT_RANDOM_NUMBER_HERE"
     border="0" alt=""></a></iframe>
    </noscript>
    </div>
    
    <!--Async ad request with multiple parameters.-->
    
    <script type="text/javascript">
        var OX_ads = OX_ads || [];
        OX_ads.push({
           "slot_id":"placeholderId",
           "auid":"8",
           "tid":"4",
           "tg":"_blank",
           "r":"http://redirect.clicks.to.here/landing.html",
           "rd":"120",
           "rm":"2",
           "imp_beacon":"HTML for client-side impression beacon",
           "fallback":"HTML for client-side fallback"
        });
    </script>
    
    <!-- Fetch the Tag Library -->
    
    <script type="text/javascript" src="http://d.example.com/w/1.0/jstag"></script>
    
    Some other content here.
    
    </body>
    </html>
    
        2
  •  5
  •   leebutts    15 年前

    我们在iFrame中加载广告以避免您遇到的问题。我们调整div和iframe的大小是一样的,iframe指向一个只包含广告片段的页面(您可以将区域和其他必需的选项作为参数传递给该页面)。

        3
  •  5
  •   Rafa    15 年前

    我们懒得加载OpenX的代码。我们没有将单页调用放在页面的顶部,而是将其放在底部。加载页面后,调用将获得横幅数据,并且自定义代码将在正确的区域中添加正确的横幅。

    这段代码将与图像,flash或HTML内容的正常横幅。它在某些情况下可能不起作用,比如使用外部提供者(adform等)的横幅。为此,你可能需要破解一点代码。

    1. 在HTML代码的末尾添加SinglePageCall代码
    2. 在SPC代码下添加此代码。
    3. 哦,是的,你需要在你的HTML代码中添加一些div作为横幅的占位符。默认情况下,我将这些横幅设置为CSS类“hidden”,它完全隐藏div(具有可见性、显示和高度)。然后,在成功加载给定DIV中的横幅之后,我们移除隐藏类,DIV(以及其中的横幅)变得可见。

    使用风险自负!:)希望有帮助

    (function(){
    if (!document || !document.getElementById || !document.addEventListener || !document.removeClass) {
    return; // No proper DOM; give up.
    }
    var openx_timeout = 1, // limit the time we wait for openx
    oZones = new Object(), // list of [div_id] => zoneID
    displayBannerAds; // function.
    
    
    // oZones.<divID> = <zoneID>
    // eg: oZones.banner_below_job2 = 100;
    // (generated on the server side with PHP)
    oZones.banner_top = 23;
    oZones.banner_bottom = 34;
    
    
    
    displayBannerAds = function(){
    if( typeof(OA_output)!='undefined' && OA_output.constructor == Array ){
      // OpenX SinglePageCall ready!
    
      if (OA_output.length>0) {
    
        for (var zone_div_id in oZones){
          zoneid = oZones[zone_div_id];
    
          if(typeof(OA_output[zoneid])!='undefined' && OA_output[zoneid]!='') {
    
            var flashCode,
              oDIV = document.getElementById( zone_div_id );
    
            if (oDIV) {
    
              // if it's a flash banner..
              if(OA_output[zoneid].indexOf("ox_swf.write")!=-1)
              {
    
                // extract javascript code
                var pre_code_wrap = "<script type='text/javascript'><!--// <![CDATA[",
                  post_code_wrap = "// ]]> -->";
    
                flashCode = OA_output[zoneid].substr(OA_output[zoneid].indexOf(pre_code_wrap)+pre_code_wrap.length);
                flashCode = flashCode.substr(0, flashCode.indexOf(post_code_wrap));
    
    
                // replace destination for the SWFObject
                flashCode = flashCode.replace(/ox\_swf\.write\(\'(.*)'\)/, "ox_swf.write('"+ oDIV.id +"')");
    
    
                // insert SWFObject
                if( flashCode.indexOf("ox_swf.write")!=-1 ){
                  eval(flashCode);
                  oDIV.removeClass('hidden');
                }// else: the code was not as expected; don't show it
    
    
              }else{
                // normal image banner; just set the contents of the DIV
                oDIV.innerHTML = OA_output[zoneid];
                oDIV.removeClass('hidden');
              }
            }
          }
        } // end of loop
      }//else: no banners on this page
    }else{
      // not ready, let's wait a bit
      if (openx_timeout>80) {
        return; // we waited too long; abort
      };
      setTimeout( displayBannerAds, 10*openx_timeout );
       openx_timeout+=4;
    }
    };
    displayBannerAds();
    })();
    
        4
  •  1
  •   Gonçalo Peres    14 年前

    <div id="openx-4"><script>wm_openx(4);</script></div>
    

    <script type="text/javascript">             
    $is_mobile = false;
    $document_ready = 0;
    $(document).ready(function() {
        $document_ready = 1;
        if( $('#MobileCheck').css('display') == 'inline' ) {
            $is_mobile = true;
            //alert('is_mobile: '+$is_mobile);
        }
    });
    
    function wm_openx($id) {
        if($is_mobile) return false;
        if(!$document_ready) {
            setTimeout(function(){ wm_openx($id); },1000);
            return false;
        }
    
        if(typeof(OA_output[$id])!='undefined' && OA_output[$id]!='') {
    
            var flashCode,
                oDIV = document.getElementById('openx-'+$id);
    
            if (oDIV) {
    
                // if it's a flash banner..
                if(OA_output[$id].indexOf("ox_swf.write")!=-1) {
    
                    // extract javascript code
                    var pre_code_wrap = "<script type='text/javascript'><!--// <![CDATA[",
                        post_code_wrap = "// ]]> -->";
    
                    flashCode = OA_output[$id].substr(OA_output[$id].indexOf(pre_code_wrap)+pre_code_wrap.length);
                    flashCode = flashCode.substr(0, flashCode.indexOf(post_code_wrap));
    
                    // replace destination for the SWFObject
                    flashCode = flashCode.replace(/ox\_swf\.write\(\'(.*)'\)/, "ox_swf.write('"+ oDIV.id +"')");
                    flashCode = flashCode.replace(/document.write/, "$('#"+ oDIV.id +"').append");
    
    
                    // insert SWFObject
                    if( flashCode.indexOf("ox_swf.write")!=-1 ) {
                        //alert(flashCode);
                        eval(flashCode);
                        //oDIV.removeClass('hidden');
                    }// else: the code was not as expected; don't show it
    
    
                }else{
                    // normal image banner; just set the contents of the DIV
                    oDIV.innerHTML = OA_output[$id];
                    //oDIV.removeClass('hidden');
                }
            }
        }
        //OA_show($id);
    }
    </script>
    
        5
  •  1
  •   patrick    13 年前

    我正在寻找这个从我的openX服务器加载广告时,广告应该是可见的。我使用的是加载在div中的iFrame版本的openX。这里的答案让我找到了解决这个问题的方法,但是发布的解决方案有点太简单了。首先,当页面没有从顶部加载时(如果用户通过单击“后退”进入页面),没有加载任何div。所以你需要这样的东西:

    $(document).ready(function(){
       $(window).scroll(lazyload);
       lazyload();
    });
    

    此外,您还需要知道什么定义了可见div,它可以是完全可见或部分可见的div。如果对象的底部大于或等于窗口的顶部,并且对象的顶部小于或等于窗口的底部,则该对象应该是可见的(或者在这种情况下:已加载)。您的函数lazyload可能如下所示:

    function lazyload(){
       var wt = $(window).scrollTop();    //* top of the window
       var wb = wt + $(window).height();  //* bottom of the window
    
       $(".ads").each(function(){
          var ot = $(this).offset().top;  //* top of object (i.e. advertising div)
          var ob = ot + $(this).height(); //* bottom of object
    
          if(!$(this).attr("loaded") && wt<=ob && wb >= ot){
             $(this).html("here goes the iframe definition");
             $(this).attr("loaded",true);
          }
       });
    }
    

    在所有主流浏览器上测试,甚至在我的iPhone上,都很有魅力!!