正如我在 DFP 帮助论坛上发布的那样:https ://productforums.google.com/forum/#!topic/dfp/9BsgVtKTU9A
我有工作解决方案,我在其中使用 jQuery。
if(typeof googletag !== 'undefined' && typeof googletag.pubads !== 'undefined') {
if(adsCloned === false) {
var $dfpIframe = $('#div-gpt-ad-1477269112222-0').find('iframe'); // this is ad id that you use in googletag.defineSlot()
$dfpIframe.each(function (i, v) {
var $clone = $(v).clone();
$(v).replaceWith($clone);
});
adsCloned = true;
}
googletag.pubads().refresh();
}
在此之前,您必须定义var adsCloned = false;
它起作用的原因是,当您刷新页面加载后插入的 iframe(在本例中为克隆 iframe)时,历史记录条目不会添加到 IE。
编辑:如果它对您不起作用,请尝试删除 if statememt:
if(typeof googletag !== 'undefined' && typeof googletag.pubads !== 'undefined') {
var $dfpIframe = $('#div-gpt-ad-1477269112222-0').find('iframe'); // this is ad id that you use in googletag.defineSlot()
$dfpIframe.each(function (i, v) {
var $clone = $(v).clone();
$(v).replaceWith($clone);
});
googletag.pubads().refresh();
}
上面的代码不起作用 - 我的错误。但对我来说可行的解决方案是销毁整个 googletag 变量并再次调用整个代码。
dfp 脚本的第一次调用如下所示(注意 googletag.pubads().disableInitialLoad(); 和 gads.id = 'dfpHeadScript';):
// Doubleclick
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function() {
var gads = document.createElement('script');
gads.async = true;
gads.id = 'dfpHeadScript';
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);
})();
googletag.cmd.push(function() {
enovatis.dfpSlots.push( googletag.defineSlot('...', [240, 400], 'div-gpt-ad-1').addService(googletag.pubads()) );
enovatis.dfpSlots.push( googletag.defineSlot('...', [[960, 100], [750, 100]], 'div-gpt-ad-2').addService(googletag.pubads()) );
googletag.pubads().enableSingleRequest();
googletag.pubads().collapseEmptyDivs();
googletag.pubads().disableInitialLoad();
googletag.enableServices();
});
googletag.cmd.push(function(){
googletag.pubads().refresh();
});
刷新广告的方法是:
var dfpInterval = null;
var $dfpTop = $('#div-gpt-ad-1');
var $dfpLeft = $('#div-gpt-ad-2');
function refreshDfp() {
$dfpTop.empty();
$dfpLeft.empty();
googletag = {};
googletag.cmd = [];
$('#dfpHeadScript').remove();
(function() {
var gads = document.createElement('script');
gads.async = true;
gads.id = 'dfpHeadScript';
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);
})();
googletag.cmd.push(function() {
enovatis.dfpSlots.push( googletag.defineSlot('...', [240, 400], 'div-gpt-ad-1').addService(googletag.pubads()) );
enovatis.dfpSlots.push( googletag.defineSlot('...', [[960, 100], [750, 100]], 'div-gpt-ad-2').addService(googletag.pubads()) );
googletag.pubads().enableSingleRequest();
googletag.pubads().collapseEmptyDivs();
googletag.pubads().disableInitialLoad();
googletag.enableServices();
window.clearInterval(dfpInterval);
dfpInterval = window.setInterval(function(){
if(typeof googletag.pubads !== 'undefined'){
window.setTimeout(function(){
googletag.pubads().refresh();
}, 75);
window.clearInterval(dfpInterval);
}
}, 75);
});
}
我称之为:refreshDfp.apply(window);
一切正常。这种方法的唯一缺点是我们每次都向 google 发送更多请求。