jQuery append Google Adsense to 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…

Leave a Comment