使用jquery在iframe中动态显示javascript google广告的问题

我们尝试在动态添加的iframe中显示Google广告。 在iframe中,字段“src”通常是一个url,但我们希望使用数据:text / html格式来直接使用我们的广告代码。 它适用于简单的JavaScript代码,如document.write(’hello world’),但不适用于google广告代码。 我们在一个html文件中模拟这个:

    function resizeFrame(f) { f.style.height = 60 + "px"; } $(document).ready(function() {; var htmlCode = document.createElement("div"); var head = document.getElementById('google_ad_468x60'); var myFrame = document.createElement("iframe") myFrame.name = "childframe" myFrame.id = "childframe" myFrame.src = "data:text/html, " + "" + ""+""+ ""+"" myFrame.width = "468" myFrame.scrolling = "no" myFrame.setAttribute('marginheight', '0px') myFrame.setAttribute('marginwidth' , '0px') myFrame.setAttribute('frameborder' , '0' ) head.appendChild(myFrame) });    

Before Google ad

After Google ad

我们在Mozilla上没有错误,但在Chrome中我们遇到此错误,在这两种情况下都不显示Google广告:

 Unsafe JavaScript attempt to access frame with URL oursitewiththetestfile.com from frame with URL file:///home/lucas/Bureau/google_ad.html from frame with URL data:text/html, google_comments . Domains, protocols and ports must match 

在Chrome的目的是阻止黑客和病毒创建者,他们不允许使用javascript访问iframe,当它与不同的域进行通信,然后是托管在其上的网站。

您将陷入这种方法,我不确定是否有解决方法。 无论哪种方式,Google广告都应直接放入网站,因为它使用内容生成相关的广告以显示在页面上。 如果您正在寻找展示自己广告的方法,则应该动态完成,使用所选代码语言编写,或者使用adsense.google.com上的广告创建者来展示您自己的广告,而不是公益广告。

我个人觉得这样会更简单。

谷歌不希望你把广告放在一个框架中,如果他们发现它就会禁止你…它在服务条款5中……