且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery 将 Google Adsense 附加到 div

更新时间:2023-09-13 16:41:28

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

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