使用 JavaScript 进行动态 Adsense 插入 [英] Dynamic Adsense Insertion With JavaScript

查看:34
本文介绍了使用 JavaScript 进行动态 Adsense 插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不敢相信这有多难找到,但即使在 Google 开发者文档中我也找不到.我需要能够动态地,only 使用 JavaScript 插入 adsense.我还查看了 StackOverflow,其他一些人也问过这个问题,但没有回应.希望这会是一个更好的解释,并会得到一些回复.

基本上,一个用户插入了我的脚本,我们称之为my.js(目前还不能说具体是什么.)my.js 已加载并且在 my.js 一些嵌入式媒体显示在他们的页面上然后我需要以某种方式附加生成的 HTML:

<script type="text/javascript"><!--google_ad_client = "ca-pub-xxx";/* my.js 示例广告 */google_ad_slot = "yyy";google_ad_width = 468;google_ad_height = 60;//--><脚本类型=文本/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js">

在特定的

(或其他)元素中.有什么想法吗?

P.S.没有像 jQuery 这样的库,我无法将 HTML 插入页面,除非它是通过 JavaScript 并且必须插入到我命名的特定 <div>(我正在使用 Sizzle 为我的 JS 库提供帮助)

解决方案

其他答案提出的用于异步加载 AdSense 脚本的简单技术将不起作用,因为 Google 使用了 document.write()在 AdSense 脚本中.document.write() 只在页面创建过程中起作用,在异步加载的脚本执行时,页面创建已经完成.

要完成这项工作,您需要覆盖 document.write(),以便在 AdSense 脚本调用它时,您可以自己操作 DOM.举个例子:

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

此技术是从此处借用的:http://blog.figmentengine.com/2011/08/google-ads-async-asynchronous.html

I can't believe how hard this is to find, but even in the Google developer docs I can't find it. I need to be able to dynamically, only with JavaScript insert adsense. I also looked on StackOverflow and some others have asked this but no response. Hopefully this will be a better explanation and will get some replies.

Basically, a user inserts my script, lets call it my.js (can't say what it is specifically at the moment.) my.js is loaded and in my.js some embedded media is displayed on their page then I need somehow to append the generated HTML from:

<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>

Inside a specific <div> (or whatever) element. Any ideas?

P.S. No libraries like jQuery, and I can't insert HTML onto the page unless it's through JavaScript and it has to be inserted into a specific <div> i named (I'm using Sizzle for my JS library if that helps)

解决方案

The simple technique used to asynchronously load the AdSense script proposed by other answers won't work because Google uses document.write() inside of the AdSense script. document.write() only works during page creation, and by the time the asynchronously loaded script executes, page creation will have already completed.

To make this work, you'll need to overwrite document.write() so when the AdSense script calls it, you can manipulate the DOM yourself. Here's an example:

<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 = 'https://pagead2.googlesyndication.com/pagead/show_ads.js';
document.body.appendChild(script);
</script>

The example first caches the native document.write() function in a local variable. Then it overwrites document.write() and inside of it, it uses innerHTML to inject the HTML content that Google will send to document.write(). Once that's done, it restores the native document.write() function.

This technique was borrowed from here: http://blog.figmentengine.com/2011/08/google-ads-async-asynchronous.html

这篇关于使用 JavaScript 进行动态 Adsense 插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆