jQuery 将 Google Adsense 附加到 div

发布于 2024-07-26 19:48:47 字数 833 浏览 9 评论 0 原文

我遇到了 google adsense 的问题,它在我的 jQuery 之前加载并杀死了我的代码,所以我想我应该尝试使用文档准备功能将 Google Adsense javascript 附加到适当的 div,这是我正在尝试的代码write:

<script language="javascript" type="text/javascript">
$(document).ready(function(){
    $(".googleBanners").html("<script language='javascript' type='text/javascript'>\n" + "google_ad_client = 'pub-8487967187298044';\n" + "google_ad_slot = '1088799521';\n" + "google_ad_width = 250;\n" + "google_ad_height = 250;\n" + "</" + "script>\n" + "<script language='javascript' src='http://pagead2.googlesyndication.com/pagead/show_ads.js' type='text/javascript'>" + "</" + "script>");
});
</script>

但是我不太擅长编写 javascript/jQuery,所以如果有人可以帮助我实现这个,那就太棒了。

我当前收到的 FF 中的错误是“错误:google_protectAndRun 未定义”。 我不确定这意味着什么,但我猜我写错了 jQuery 代码.. 哈哈

I'm having issues with google adsense and it loading before my jQuery and killing my codes, so I thought I'd try to append the Google Adsense javascript to the appropriate div using the document ready function, here's the code I'm trying to write:

<script language="javascript" type="text/javascript">
$(document).ready(function(){
    $(".googleBanners").html("<script language='javascript' type='text/javascript'>\n" + "google_ad_client = 'pub-8487967187298044';\n" + "google_ad_slot = '1088799521';\n" + "google_ad_width = 250;\n" + "google_ad_height = 250;\n" + "</" + "script>\n" + "<script language='javascript' src='http://pagead2.googlesyndication.com/pagead/show_ads.js' type='text/javascript'>" + "</" + "script>");
});
</script>

But I'm not so good writing javascript/jQuery so if someone could help me implement this that would be fantastic.

The error in FF I'm currently getting is "Error: google_protectAndRun is not defined". I'm not sure what that means, but I'm guessing I've written the jQuery code wrong.. lol

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

屋檐 2024-08-02 19:48:47

我这样做的方法是在我希望广告出现的地方放置一个占位符。

<html>
   <body>
      <div id="googleadgoeshere"></div>
   </body>
</html>

然后将谷歌代码放入页面末尾的容器中。

<div id="adsense" style="display:none;">all the google javascript goes here</div>

然后,我使用 jQuery 移动页面加载完成后 adsense 代码创建的 iframe。

$(window).load(function(){
    $("#adsense").find("iframe").appendTo("#googleadgoeshere"); 
    $("#adsense").remove();
});

如果您只是尝试移动 #adsense div,您最终会得到一个空白页面。 如果您尝试以任何其他方式执行此操作,您最终会得到一个空白页。
谷歌已经建立了主动的方法来检查代码是否没有被移动。 如果是这样,您的页面将为空白。 为什么谷歌这样做超出了我的范围,但我发现这个解决方法对我有用......

The way I do this is by having a placeholder on the spot I want the ad to appear.

<html>
   <body>
      <div id="googleadgoeshere"></div>
   </body>
</html>

Then place the google-code in a container at the end of the page.

<div id="adsense" style="display:none;">all the google javascript goes here</div>

I then use jQuery to move the iframe the adsense code creates once the page is done loading.

$(window).load(function(){
    $("#adsense").find("iframe").appendTo("#googleadgoeshere"); 
    $("#adsense").remove();
});

If you just try to move the #adsense div you will end up with a blank page. If you try to do this most any other way, you will end up with a blank page.
Google had built in active ways to check that the code is not moved. If it is, your page will be blank. Why google has done this is beyond me, but I have found this workaround to work for me...

盗梦空间 2024-08-02 19:48:47

您不能以这种方式包含外部脚本。

要在页面加载后包含 javascript,您应该使用 jQuery 的 jQuery.getScript() 函数,但我不知道这是否适用于 Google Adsense。

可以在这里找到更多信息:

http://geek.littleredstring.com/17-load-adsense-last-jquery

You can't include external scripts that way.

To include javascript after the page has loaded, you should use jQuery's jQuery.getScript() function, but I don't know if that would work for Google Adsense.

A little more info can be found here:

http://geek.littleredstring.com/17-load-adsense-last-jquery

海之角 2024-08-02 19:48:47

在尝试使用此处的代码但失败后,我最终采用了我正在使用的 jQuery 对象并将其放入新的 html 页面中。 完成此操作后,我只需使用 iframe 将其放置在包含 adsense 的页面上。

现在,adsense 和 jQuery 同时运行没有任何问题。

After trying and failing with the codes on here, I ended up taking the jQuery object that I was using and putting it in a new html page. Once I did that, I just used an iframe to place it on the page with the adsense.

Now, adsense and jQuery run at the same time with no problems.

北斗星光 2024-08-02 19:48:47

在页面中的某处添加带有 adsense 代码的隐藏 DIV:

<div id='adsense' style="display:none">
    <script type="text/javascript"><!--
        google_ad_client = "ca-pub-xxxxxxxxxxxxxx";
        google_ad_slot = "xxxxxxxxxx";
        google_ad_width = 300;
        google_ad_height = 250;
        //-->
    </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</div>

使用 javascript 创建动态 DIV 供您的广告加载:

$("body").append("<div id='adslot' ></div>");

用于插入广告的 Jquery 代码:

var ad = $("#adsense").html();
$("#adslot").html(ad);

这对我有用。

Add a hidden DIV with adsense code in your page somewhere:

<div id='adsense' style="display:none">
    <script type="text/javascript"><!--
        google_ad_client = "ca-pub-xxxxxxxxxxxxxx";
        google_ad_slot = "xxxxxxxxxx";
        google_ad_width = 300;
        google_ad_height = 250;
        //-->
    </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</div>

Using javascript create dynamic DIV for your ad to load:

$("body").append("<div id='adslot' ></div>");

Jquery code to insert the Ad:

var ad = $("#adsense").html();
$("#adslot").html(ad);

This works for me.

捂风挽笑 2024-08-02 19:48:47

我有同样的问题。 我最终通过使用 jQuery 加载一个指向包含 Google AdSense javascript 的 html 文件的 src 解决了这个问题。 这是相当不雅观的,因为 Google AdSense 代码创建了一个

I had this exact same problem. I eventually solved it by using jQuery to load an with a src pointing to an html file that contains the Google AdSense javascript. This is rather inelegant, since the Google AdSense code creates an <iframe>. And, I happen to be working with a Facebook application, so in my case, I've got an <iframe> (the Google Ad) in an <iframe> (the one I used to get around the Firefox error) in an <iframe> (my Facebook app). But, it works.

厌味 2024-08-02 19:48:47

在我自己进行了广泛的实验失败之后,丹尼的回答解释了这个问题的基本解决方案,所以谢谢! 然而,我需要一个更复杂的解决方案,因为我想要在给定页面上替换未知数量的广告,所以这就是我所做的:

基本的 html (php) 大纲,ala Danny 的格式 - 请注意我递增的广告计数,基于数据库查询中的行的各种因素,即不可能事先知道计数:

<html>
   <body>
<? while ($r = mysql_fetch_assoc($rs)) { if (true) { ?>
      <div class="adslide"><?=$ads++?></div>
<? } } ?>
   </body>
</html>

我分离出了​​我将立即创建的 adsense div 的 css,因为每个 adslide div 都有一个上面创建的:

<style> .adsense { display: none; } </style>

在这里,放置在页面底部,我将来自 google 的实际广告放入 html 中,计数由我在上面为它们提供的插槽数量决定:

<?php for ($i = 0; $i < $ads; $i++) { echo '<div class="adsense">'.$adscript.'</div>'; } ?>

最后,我循环遍历写入的所有 adsense 广告html 并将它们一一填充到在 html 中创建的 adslide 插槽中,确保每个广告和插槽仅使用/填充一次,方法是在我完成它们后删除它们或其类:

<script>
// http://stackoverflow.com/questions/1142861/jquery-append-google-adsense-to-div
$(function () { var b, a = $(".adsense").first();
 for (; a.length > 0; a = $(".adsense").first())
 { b = $(".adslide").first(); b.append(a.find("iframe"));
  a.remove(); b.removeClass("adslide"); } });
</script>

这是来自谷歌的一个极其奇怪的错误。 我只能假设它与谷歌创建的一些保护有关,这些保护是为了防止人们隐藏他们的广告(通过将它们放置在屏幕外或其他 html 元素或其他东西后面)来尝试收集展示次数而不实际展示广告(即,这样你就可以放置一百万这些在 html 中,但用户永远不会看到它们,并且您可以收取现金,直到谷歌发现)。 然而,事实上这个错误并没有出现在 IE 和 Safari 中,而是出现在 Firefox 和 Google 自己的 Chrome 中……这很奇怪。 他们绝对应该解决这个问题。

对于那些使用相同软件的人:我自己在实现 jQuery Carousel (http://sorgalla.com/projects/jcarousel/) 时遇到了这个问题,该轮播中的广告与用户提交的照片混合在一起。

Danny's answer explained the basic solution for this after I unsuccessfully experimented extensively on my own, so thanks! However, I needed a more elaborate solution as I had an unknown number of ads that I wanted to replace on a given page, so here's what I did:

The basic html (php) outline, ala Danny's format -- note my incrementing of the ad count, based on various factors of rows from a db query, i.e. such that it is impossible to know the count beforehand:

<html>
   <body>
<? while ($r = mysql_fetch_assoc($rs)) { if (true) { ?>
      <div class="adslide"><?=$ads++?></div>
<? } } ?>
   </body>
</html>

I separated out the css for the adsense div I'll create in a moment as I have one for each adslide div created above:

<style> .adsense { display: none; } </style>

Here, placed in the bottom of the page, I get the actual ads from google into the html, the count being determined by how many slots I have for them from above:

<?php for ($i = 0; $i < $ads; $i++) { echo '<div class="adsense">'.$adscript.'</div>'; } ?>

And finally I cycle through all the adsense ads written into the html and fill them in, one by one, into the adslide slots that were created in the html, making sure each ad and slot are only used/filled once by removing them or their class after I'm done with them:

<script>
// http://stackoverflow.com/questions/1142861/jquery-append-google-adsense-to-div
$(function () { var b, a = $(".adsense").first();
 for (; a.length > 0; a = $(".adsense").first())
 { b = $(".adslide").first(); b.append(a.find("iframe"));
  a.remove(); b.removeClass("adslide"); } });
</script>

This is an extremely weird bug from google. I can only assume it's related to some protection google created to prevent people from hiding their ads (by positioning them offscreen or behind other html elements or something) to try and collect impression counts without actually displaying ads (i.e. so you could put a million of these in the html but the user would never see them, and you collect the cash until google finds out). However, the fact that this bug does not show up in IE and Safari but does in Firefox and Google's own Chrome... That's weird. They should definitely fix this on their side.

For those who are working with the same software: I ran into this problem myself when implementing a jQuery Carousel (http://sorgalla.com/projects/jcarousel/) that had ads mingled with user submitted photos in the carousel.

复古式 2024-08-02 19:48:47

仅此代码有效

var i = 0;
obj = $(".materialContent p");
size = obj.size();
for (i=0; i<size; i++)
{
    cur = obj[i];
    if (i == 2)
    {
        $("#mainTopAdvDiv").appendTo(cur); 
    }
}

不要删除、移动整个 div。

Only this code working

var i = 0;
obj = $(".materialContent p");
size = obj.size();
for (i=0; i<size; i++)
{
    cur = obj[i];
    if (i == 2)
    {
        $("#mainTopAdvDiv").appendTo(cur); 
    }
}

Do not remove, move full div.

南…巷孤猫 2024-08-02 19:48:47

在通过 ajax 加载的页面的 link 中,我设置了 data-ajax="false" ,然后在页面上已加载 我要求用户使用后退按钮。

In the link to the page that was being loaded via ajax, I set data-ajax="false" and then on the page that was loaded I ask the user to use the back button.

墨落画卷 2024-08-02 19:48:47

我就是这样做的。 简单又简短。

<script>
$(window).load(function(){
    $('#adsense').fadeIn(0);
});
</script>

<div id="adsense" style="display:none;">
   [YOUR ADSENSE SCRIPT HERE]
</div>

This is how I would do it. Simple and short.

<script>
$(window).load(function(){
    $('#adsense').fadeIn(0);
});
</script>

<div id="adsense" style="display:none;">
   [YOUR ADSENSE SCRIPT HERE]
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文