如何仅在我的网站未包含在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广告呈现脚本。
正在寻找一种不同的方法来解决这个问题。
答
Try
var isInIframe = (window.parent !=null && window.location != window.parent.location) ? true : false;
if (!isInIframe)
loadScript("http://your.cdn.com/show_ads.js");
试试这个加载一个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);
}
您是否尝试过'window.parent == null'?这意味着你不在iframe中。 – Diego
@Diego - 'window.parent'永远不会是'null'。在顶层框架中,'window.parent == window' ...这或多或少是在问题中检查的内容。 – Quentin
我怀疑你的问题是,'$ .getScript(“show_ads.js中”);'是不行的,但我们不能看到* show_ads.js中的代码*。 – Quentin