如何仅在我的网站未包含在iFrame中时才显示AdSense?

问题描述:

我有一个网站,这也是Facebook上的画布应用程序。根据FB政策,我无法在画布页面上显示Adsense广告。另外根据Google Adsense的政策,我无法在iFrame中显示Adsense,FB用它将我的网站显示为画布应用。我的网站使用Adsense进行货币化。如何仅在我的网站未包含在iFrame中时才显示AdSense?

我需要一个简单的解决方案,以使只有当我的网站没有的iFrame无论是作为一个帆布的应用程序或通过任何其他方式我的AdSense广告。

我尝试使用jQuery和做了以下

if (window==window.top) { 
    $.getScript("show_ads.js"); 
} 

但不会呈现在任何情况下的广告。 show_ads.js是Google提供的Adsense广告呈现脚本。

正在寻找一种不同的方法来解决这个问题。

+2

您是否尝试过'window.parent == null'?这意味着你不在iframe中。 – Diego

+0

@Diego - 'window.parent'永远不会是'null'。在顶层框架中,'window.parent == window' ...这或多或少是在问题中检查的内容。 – Quentin

+0

我怀疑你的问题是,'$ .getScript(“show_ads.js中”);'是不行的,但我们不能看到* show_ads.js中的代码*。 – Quentin

Try 

    var isInIframe = (window.parent !=null && window.location != window.parent.location) ? true : false; 
     if (!isInIframe) 
     loadScript("http://your.cdn.com/show_ads.js"); 

reference

试试这个加载一个js文件:

function loadScript(url, callback){ 

    var script = document.createElement("script") 
    script.type = "text/javascript"; 

    if (script.readyState){ //IE 
     script.onreadystatechange = function(){ 
      if (script.readyState == "loaded" || 
        script.readyState == "complete"){ 
       script.onreadystatechange = null; 
       callback(); 
      } 
     }; 
    } else { //Others 
     script.onload = function(){ 
      callback(); 
     }; 
    } 

    script.src = url; 
    document.getElementsByTagName("head")[0].appendChild(script); 
} 
+0

如果页面是由不同原点构成的,则会抛出异常并且脚本将终止。如果它在测试之后做了任何事情,看看是否应该包含广告,那就会中断。 – Quentin

+0

为什么要用'parent'代替'top'或者添加'location'来解决问题呢? – Quentin

+0

你可以使用css选择器来隐藏它在iframe中吗?例如,不知道您的实际clases和id的:正常 - .adsense {显示:块;}上FB - #FB .adsense {显示:无;} – DrCord