2011-06-01 95 views
21

我不敢相信这是多么难找到,但即使在Google开发人员文档中我也找不到它。我需要能够动态地,只有与JavaScript插入AdSense。我也看着StackOverflow和其他一些人问这个,但没有回应。希望这将是一个更好的解释,并会得到一些答复。使用JavaScript动态Adsense插入

基本上,用户插入我的剧本,让我们把它叫做my.js(不能说具体是在什么时刻)my.js被加载并在my.js一些嵌入式媒体显示自己的页面上,然后我需要以某种方式追加从所生成的HTML:

<script type="text/javascript"><!-- 
google_ad_client = "ca-pub-xxx"; 
/* my.js example Ad */ 
google_ad_slot = "yyy"; 
google_ad_width = 468; 
google_ad_height = 60; 
//--> 
</script> 
<script type="text/javascript" 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 

内部的特定<div>(或其他)元件。有任何想法吗?

P.S.像jQuery没有图书馆,我不能插入到HTML页面,除非它是通过JavaScript和它必须被插入到特定的<div>我叫(我用Sizzle我的JS库是否有帮助)

回答

2

怎么样具有瓦尔(省略google_ad_client等)始终在头部和动态追加另一部分是这样的:

var script = document.createElement('script'); 
script.type = 'text/javascript'; 
script.src = 'http://pagead2.googlesyndication.com/pagead/show_ads.js'; 
myDIV.appendChild(script); 
0

this page,有可能生成脚本标记并填充上飞的SRC领域 - 这是@noiv建议的(我的版本应该是独立的;不需要外部html或js库)。你试过这个吗?它似乎不那么难......

function justAddAdds(target_id, client, slot, width, height) { 
    // ugly global vars :-P 
    google_ad_client = client; 
    google_ad_slot = slot; 
    google_ad_width = width; 
    google_ad_height = height; 
    // inject script, bypassing same-source 
    var target = document.getElementById(target_id); 
    var script = document.createElement('script'); 
    script.type = 'text/javascript'; 
    script.src = 'http://pagead2.googlesyndication.com/pagead/show_ads.js'; 
    target.appendChild(script); 
} 
+0

这是很久以前的事,但与动态插入它的一个问题。我确定在另一个SO线程中存在这个原因,但是,无论如何,它在一年前都不适用于adsense脚本。你最近试过了吗? – 2012-08-01 19:10:46

+0

没有与AdSense,但我刚刚证实,这可以让你执行Jsfiddle内的任意外部JS ... – tucuxi 2012-08-01 19:13:45

+0

是的:)我知道它适用于随机文件,但有一个具体原因,它不适用于AdSense我现在不记得了,因为它已经有一段时间了 – 2012-08-01 19:19:34

18

用于异步加载其他的答案提出的AdSense的脚本将无法工作,因为谷歌使用document.write()的在AdSense脚本内的简单的技术。 document.write()仅在页面创建期间有效,并且在执行异步加载的脚本时,页面创建将已完成。

为了完成这项工作,您需要覆盖document.write(),以便在AdSense脚本调用它时,您可以自己操作DOM。下面是一个示例:

<script> 
window.google_ad_client = "123456789"; 
window.google_ad_slot = "123456789"; 
window.google_ad_width = 200; 
window.google_ad_height = 200; 

// container is where you want the ad to be inserted 
var container = document.getElementById('ad_container'); 
var w = document.write; 
document.write = function (content) { 
    container.innerHTML = content; 
    document.write = w; 
}; 

var script = document.createElement('script'); 
script.type = 'text/javascript'; 
script.src = 'http://pagead2.googlesyndication.com/pagead/show_ads.js'; 
document.body.appendChild(script); 
</script> 

该示例首先将本地document.write()函数缓存到局部变量中。然后它覆盖document.write()并在其内部使用innerHTML注入Google将发送到document.write()的HTML内容。一旦完成,它将恢复本地document.write()函数。

这项技术是从这里借:http://blog.figmentengine.com/2011/08/google-ads-async-asynchronous.html

+0

尚未测试它,但它听起来很合理。很好的解决方法。另一方面,这种技术可以通过谷歌广告代码中的适当编程来预先... – tucuxi 2013-04-07 20:56:43

+0

谢谢,它为我工作!你救了我的一天:) – 2013-05-06 05:27:52

-1

这些方法将工作,但他们不会为HTTPS工作。如果您想动态放置广告并使用https,则需要注册DoubleClick For Publishers。

我在我的网站上有这个问题,所以我把npm模块放在一起来解决这个问题。希望你觉得它有用。

Use Adsense in Single Page Web Apps

该链接包含如何使用该模块在一个页面的Web应用程序完整的示例代码。

一旦你安装了这个代码将显示在元素广告指定的模块: ViceView.showAdsense({adslot: {slot: "myslot", sizes: "[300,100]", id:"your adsenseid"}, element: "element", adwidth: 300, adheight: 100});

2

我已经有AdSense的我的网页上也注入了新的广告到我的博客文章中的占位符。在我想要广告注入的地方,我添加了一个“adsense-inject”类的div,然后在文档准备就绪时运行此脚本,并且我知道adsense脚本已经为其他广告加载: -

$(document).ready(function() 
    { 
     $(".adsense-inject").each(function() { 
      $(this).append('<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3978524526870979" data-ad-slot="6927511035" data-ad-format="auto"></ins>'); 
      (adsbygoogle = window.adsbygoogle || []).push({}); 
     }); 
    }); 
0

这是一个更新的实现,如果您不需要管理使用常见的外部javascript包含的广告,这是非常有用的,在我的情况下,我有很多静态html文件,并且它工作正常。它还为我的AdSense脚本提供了单点管理。用法

var externalScript = document.createElement("script"); 
externalScript.type = "text/javascript"; 
externalScript.setAttribute('async','async'); 
externalScript.src = "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"; 
document.getElementsByTagName('body')[0].appendChild(externalScript); 

var ins = document.createElement("ins"); 
ins.setAttribute('class','adsbygoogle'); 
ins.setAttribute('style','display:block;');/*add other styles if required*/ 
ins.setAttribute('data-ad-client','ca-pub-YOUR-CLIENTID'); 
ins.setAttribute('data-ad-slot','YOUR-SLOTID'); 
ins.setAttribute('data-ad-format','auto'); 
document.getElementsByTagName('body')[0].appendChild(ins); 

var inlineScript = document.createElement("script"); 
inlineScript.type = "text/javascript"; 
inlineScript.text = '(adsbygoogle = window.adsbygoogle || []).push({});' 
document.getElementsByTagName('body')[0].appendChild(inlineScript); 

例子:

<script type="text/javascript" src="/adinclude.js"></script>