2012-04-11 107 views
2

好吧,我正在一个响应式网站上工作,我试图以尽可能最好的方式处理Adsense,这不会让我被禁止!我想要做的只是在浏览器宽度小于533px的情况下,使用jQuery添加我的Adsense代码。通过这种方式,我可以展示适合窗口的较小广告,而不会破坏布局。我在添加Adsense javascript时遇到了一些麻烦。这工作:使用jQuery将Javascript代码插入DIV

(function($){ 
//detect the width on page load 
$(document).ready(function(){ 
var current_width = $(window).width(); 
//do something with the width value here! 
if(current_width < 533){ 
     $('.ads').prepend("THIS IS WHERE I WANT THE ADSENSE CODE TO GO"); 
} 
}); 

但是当我包括我的AdSense代码来代替。这就是我想要的ADSENSE去,事实并非如此。这是AdSense代码:

<script type="text/javascript"><!-- 
google_ad_client = "ca-pub-1234567890"; 
/* Test Ad */ 
google_ad_slot = "1234567890"; 
google_ad_width = 250; 
google_ad_height = 250; 
//--> 
</script> 
<script type="text/javascript" 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 

我也试着包括JavaScript代码在一个.html文件,并使用jQuery.get和jQuery.getScript一个.html和.js文件Adsense的。我只是无法让它工作。

我可以用一个简单的PHP头部检测来实现,但我希望广告根据宽度显示,而不是设备类型。

有人知道这可以做到吗?

(转贴因为没有答复前面的问题)

+0

当你说它不起作用,你是什么意思?你会遇到某种错误吗? – Rodolfo 2012-04-11 18:40:25

+0

没有东西被添加到div中。当它只是纯文本时,它会被添加。 – Ian 2012-04-11 18:56:14

回答

0

您面临的问题是Google并不真正支持AdSense ajax。包含AdSense代码的正常方式使用它所在页面的URL来呈现正确的内容。

现在,假设您可以将AdSense代码减少到特定的URL。你可以做这样的事情:

// note, this url is just a suggestion, not something that will likely work 

var url = 'http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-1234567890&ad_slot=1234567890&ad_width=250&ad_height=250'; 

var adsContainer = $('.ads'); // though, if there's only one, use id, not class 

$.ajax({ 
type:'GET', 
    url:url, 
    async:true, 
    success:function(html) { 
     adsContainer.html(html); 
     return false; 
    }, 
    error: function(html) { 
     adsContainer.html('SOME DEFAULT AD MESSAGE'); 
     return false; 
    } 
}); 

但我认为这几乎肯定会让你陷入困境与谷歌。

谷歌曾经有一个AdSense AJAX程序:

https://developers.google.com/adsense-for-ajax/

但现在是 “不可用”。你不能将你的代码放在你自己的服务器上的外部.html或.php文件和ajax中的原因是,javascript不会被ajax调用的客户端引擎执行。您正在将原始JavaScript注入到已由JavaScript引擎解释的页面中。

您可以使用node.js或某种服务器端JavaScript解析器来为您的.html文件提供服务吗?也许吧,但这对解决这个问题有很大的帮助。

我的建议是这样的:包括多个较小的广告,如果放在大页面上会看起来不错,或者如果在一个小页面上挤在一起,则会平铺。

1

我不知道这是否会工作或没有,但它是值得一试:

/* these should be global */ 
google_ad_client = "ca-pub-1234567890"; 
/* Test Ad */ 
google_ad_slot = "1234567890"; 
google_ad_width = 250; 
google_ad_height = 250; 

$(function(){ 
    //detect the width on page load 
    var current_width = $(window).width(); 
    //do something with the width value here! 
    if(current_width < 533){ 
     $.getScript('http://pagead2.googlesyndication.com/pagead/show_ads.js'); 
    } 
}); 
0

你试过提单外部的.js喜欢这个?

var script = document.createElement('script'); 
    script.src = 'http://pagead2.googlesyndication.com/pagead/show_ads.js'; 
    script.type = 'text/javascript'; 
    var head = document.getElementsByTagName("head")[0]; 
    head.appendChild(script); 
+0

OP正在使用jQuery寻找答案。 – 2012-04-11 18:45:01

+0

我的问题是,根据谷歌的T和C,代码必须是正确的,因为他们提供它,否则它可能导致禁令。我会测试这些建议并回报。 – Ian 2012-04-11 18:57:31