问题描述
我正在尝试设置新元素的 html 内容,但是当我检查 slot.getHtml() 是否给我 html 元素(如下设置)时,它显示一个空字符串。知道为什么会发生这种情况吗?
var slot = googletag.defineslot('/1234567/sports',[160,600],'ozge')
.addService(googletag.companionAds())
.addService(googletag.pubads())
.addService(googletag.content());
googletag.content().setContent(slot,'<h2>Custom content in ad slot.</h2>')
我使用此链接中的代码片段查看新定义的插槽:https://gist.github.com/rdillmanCN/a70ec955d9a982127fefadabe8b898b5
解决方法
详细说明here,googletag.content()
用于手动设置插槽的内容。它在生成的广告调用 iframe 之前将 html 内容添加到目标广告位 div 中:
var slot = googletag.defineSlot('adpath',[728,90],'div-1')
.addService(googletag.content())
.addService(googletag.pubads());
var content = '<a href="www.mydestinationsite.com"><img src="www.mysite.com/img.png"></img></a>';
googletag.content().setContent(slot,content);
HTML 结果:
slot.getHtml()
将提取生成的 iframe 中的 html。如果它返回空字符串,则表示您的广告位广告调用为空。它不提取 googletag.content()
,而是提取 google 广告 iframe innerHTML。这就是它返回空字符串的原因。
警告:根据我的经验,googletag.content().setContent()
仅在 adcall 不返回任何显示内容(空 adcall)时才会设置所需的 html。只要显示横幅,您的 googletag.content() 就会被清除:
据我所知,googletag.content().setContent()
用于设置 html 在您的广告调用完成之前或如果您的广告调用没有返回任何显示内容。
总结:slot.getHtml()
无法获取生成的广告 iframe 之外的任何内容。当广告调用返回空展示时,它返回一个空字符串。