问题描述
我遇到了 google adsense 问题,它在我的 jQuery 之前加载并杀死了我的代码,所以我想我会尝试使用文档就绪功能将 Google Adsense javascript 附加到适当的 div,这是我的代码我想写:
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>
但我不太擅长编写 javascript/jQuery,所以如果有人能帮我实现这个,那就太棒了.
But I'm not so good writing javascript/jQuery so if someone could help me implement this that would be fantastic.
我目前在 FF 中遇到的错误是错误:google_protectAndRun 未定义".我不确定这意味着什么,但我猜我写错了 jQuery 代码..lol
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
推荐答案
我这样做的方法是在我希望广告出现的地方放置一个占位符.
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>
然后将 google-code 放在页面末尾的容器中.
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>
然后,我使用 jQuery 移动页面加载完成后 Adsense 代码创建的 iframe.
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();
});
如果您只是尝试移动#adsense div,您最终会得到一个空白页面.如果您尝试以任何其他方式执行此操作,最终将得到一个空白页面.谷歌已经建立了积极的方式来检查代码没有被移动.如果是,您的页面将是空白的.为什么谷歌这样做超出了我的理解,但我发现这个解决方法对我有用......
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...
这篇关于jQuery 将 Google Adsense 附加到 div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!