javascript - 加载后广告 - 使用appendChild移动带有文本链接广告的IFRAME

标签 javascript iframe replace adsense

我已经更改了代码,基本上加载了一个隐藏 div 中的页面底部,并附加了一个名为 document.getElementById(xxx).appendChild() 的 onload 事件处理程序,以获取隐藏广告并将其移动到在我的页面的正确位置。这很好用。但是,当广告是文本广告时,在我移动广告后,渲染的 Iframe 中没有任何内容。在移动它之前,我做了测试,看看它是什么样子,果然文本链接加载到了 IFRAME 中,但第二次我执行了appendChild 调用来移动包含广告的 div,我似乎丢失了 Iframe 的内容。

有什么想法发生了什么

<div id="myad" style="display: none;">
    GA_googleFillSlot("MyADSlotName");
</div>

<script>
window.onload = function() {
    // leader board
    document.getElementById('adplaceholder').appendChild(document.getElementById('myAd'));
    document.getElementById('myAd').style.display = '';
</script>

更新:我认为这里的问题是,在文字广告上,谷歌直接写入 iframe,在其他广告上插入相关的文字链接,它使用 iframe 只指向某些 src。似乎当我执行appendchild时,如果复制完成后没有为iframe设置“src”,则新位置中的iframe不包含任何内容...猜猜它会在src上重新加载?有什么办法解决这个问题吗?

最佳答案

这应该可以解决问题:

<div id="adsense_empty">placeholder</div>
<div id="adsense_content" style="display: none;">
    <script src="adsense">...</script>
</div>

<script>
function replace( oldel, newel, show ) {
  if ( typeof newel == "string" ) 
    newel = document.getElementById( newel );
  if ( typeof oldel == "string" ) 
    oldel = document.getElementById( oldel );
  if ( newel && oldel )
    oldel.parentNode.replaceChild( newel, oldel );
  if ( show ) 
    newel.style.display = "";
}

window.onload = function() {
    replace( "adsense_empty",  "adsense_content",  true );
    replace( "adsense_empty2", "adsense_content2", true );
    replace( "adsense_empty3", "adsense_content3", true );
};
</script>

关于javascript - 加载后广告 - 使用appendChild移动带有文本链接广告的IFRAME,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2815563/

相关文章:

javascript - 优化谷歌应用程序脚本中的循环

正则表达式:如何替换模式的一部分并引用其中的变量?

javascript - jQuery/javascript 对象传递

javascript - 如何使用类名获取一个div的内容并使用类名将它们附加到另一个div

javascript - 隔离 iframe session

javascript - 更改Wordpress视频插件以自动设置youtube iframe参数

iframe - 嵌套在非安全页面上的安全 IFRAME

mysql - MySQL 上的 URL 正则表达式搜索和替换(在 WordPress 中)

java - ReplaceAll 和正则表达式组‽

Javascript -> 添加同名嵌套对象的值