2012-09-30 288 views
0

我使用的是DoubleClick for Publishers (DFP) ads,但是當我使用jquery append時,在一個div中加載DoubleClick for Publishers (DFP) ads,廣告區塊爲空。如何做到這一點,使廣告可以顯示nomorlly?jquery在一個div中附加DFP廣告管理系統(DFP)廣告

Demo link

感謝。

<?php header("Content-type: text/html; charset=utf-8"); ?> 
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"> 
<?php if(!$_POST['number']){ ?> 
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js?ver=1.7.2'></script> 
<script type='text/javascript'> 
    var googletag = googletag || {}; 
    googletag.cmd = googletag.cmd || []; 
    (function() { 
    var gads = document.createElement('script'); 
    gads.async = true; 
    gads.type = 'text/javascript'; 
    var useSSL = 'https:' == document.location.protocol; 
    gads.src = (useSSL ? 'https:' : 'http:') + 
    '//www.googletagservices.com/tag/js/gpt.js'; 
    var node = document.getElementsByTagName('script')[0]; 
    node.parentNode.insertBefore(gads, node); 
    })(); 
</script> 
<script type='text/javascript'> 
    googletag.cmd.push(function() { 
     googletag.pubads().enableAsyncRendering(); 
     googletag.defineSlot('/17177879/ads_300_250_white_right', [300, 250], 'div-gpt-ad-1-1').addService(googletag.pubads()); 
     googletag.pubads().enableSingleRequest(); 
     googletag.enableServices(); 
    }); 
</script> 
<script type="text/javascript"> 
jQuery(document).ready(function(){ 
    $(".bnews").live('click',function(){ 
     var nnum = $(this).attr('rel'); 
     googletag.cmd.push(function() { 
      googletag.pubads().enableAsyncRendering(); 
      googletag.defineSlot('/17177879/ads_300_250_white_right', [300, 250], 'div-gpt-ad-' + nnum + '-1').addService(googletag.pubads()); 
      googletag.pubads().enableSingleRequest(); 
      googletag.enableServices(); 
     }); 
     $(this).css('display','none'); 
     $.ajax({ 
      url: "1.php", 
      dataType: "html", 
      type: 'POST', 
      data: 'number=' + nnum, 
      success: function(data){ 
       $("#cc-wrap").append(data); 
      }    
     }); 
    }); 
}); 
</script> 
<h1 class="bnews" rel="2">Append to div</h1> 
<!-- some content here --> 
<!-- ads_300_250_white_right --> 
<div id='div-gpt-ad-1-1' style='width:300px; height:250px;'> 
<script type='text/javascript'> 
googletag.cmd.push(function() { googletag.display('div-gpt-ad-1-1'); }); 
</script> 
<div id="cc-wrap"></div> 
<?php }else{ ?> 
<h1 class="bnews" rel="<?php echo ($_POST['number']+1); ?>">Append to div</h1> 
<!-- some content here --> 
<!-- ads_300_250_white_right --> 
<div id='div-gpt-ad-<?php echo $_POST["number"]; ?>-1' style='width:300px; height:250px;'> 
<script type='text/javascript'> 
googletag.cmd.push(function() { googletag.display('div-gpt-ad-<?php echo $_POST["number"]; ?>-1'); }); 
</script> 
<?php } ?> 

回答

1

您需要在兩個通話刪除對googletag.pubads()。enableSingleRequest()函數...它看起來像這樣從請求更多的廣告已追加後阻止你。當我在兩個地方註釋到你正在使用的那段代碼時,腳本開始工作......但它仍然有點破碎。

這是註釋,這樣它的工作線的例子:

<?php header("Content-type: text/html; charset=utf-8"); ?> 
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"> 
<?php if(!$_POST['number']){ ?> 
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js?ver=1.7.2'></script> 
<script type='text/javascript'> 
    var googletag = googletag || {}; 
    googletag.cmd = googletag.cmd || []; 
    (function() { 
    var gads = document.createElement('script'); 
    gads.async = true; 
    gads.type = 'text/javascript'; 
    var useSSL = 'https:' == document.location.protocol; 
    gads.src = (useSSL ? 'https:' : 'http:') + 
    '//www.googletagservices.com/tag/js/gpt.js'; 
    var node = document.getElementsByTagName('script')[0]; 
    node.parentNode.insertBefore(gads, node); 
    })(); 
</script> 
<script type='text/javascript'> 
    googletag.cmd.push(function() { 
     googletag.pubads().enableAsyncRendering(); 
     googletag.defineSlot('/17177879/ads_300_250_white_right', [300, 250], 'div-gpt-ad-1-1').addService(googletag.pubads()); 
     //googletag.pubads().enableSingleRequest(); 
     googletag.enableServices(); 
    }); 
</script> 
<script type="text/javascript"> 
jQuery(document).ready(function(){ 
    $(".bnews").live('click',function(){ 
     var nnum = $(this).attr('rel'); 
     googletag.cmd.push(function() { 
      googletag.pubads().enableAsyncRendering(); 
      googletag.defineSlot('/17177879/ads_300_250_white_right', [300, 250], 'div-gpt-ad-' + nnum + '-1').addService(googletag.pubads()); 
      //googletag.pubads().enableSingleRequest(); 
      googletag.enableServices(); 
     }); 
     $(this).css('display','none'); 
     $.ajax({ 
      url: "1.php", 
      dataType: "html", 
      type: 'POST', 
      data: 'number=' + nnum, 
      success: function(data){ 
       $("#cc-wrap").append(data); 
      } 
     }); 
    }); 
}); 
</script> 
<h1 class="bnews" rel="2">Append to div</h1> 
<!-- some content here --> 
<!-- ads_300_250_white_right --> 
<div id='div-gpt-ad-1-1' style='width:300px; height:250px;'> 
<script type='text/javascript'> 
googletag.cmd.push(function() { googletag.display('div-gpt-ad-1-1'); }); 
</script> 
<div id="cc-wrap"></div> 
<?php }else{ ?> 
<h1 class="bnews" rel="<?php echo ($_POST['number']+1); ?>">Append to div</h1> 
<!-- some content here --> 
<!-- ads_300_250_white_right --> 
<div id='div-gpt-ad-<?php echo $_POST["number"]; ?>-1' style='width:300px; height:250px;'> 
<script type='text/javascript'> 
googletag.cmd.push(function() { googletag.display('div-gpt-ad-<?php echo $_POST["number"]; ?>-1'); }); 
</script> 
<?php } ?> 

讓我知道,如果你有任何問題。

0

只需將廣告代碼塊放入iframe中即可使用,您不能僅複製Google廣告代碼腳本

相關問題