Finally, use JavaScript to move each ad to its proper place. This script uses document.getElementById
to find the ad and the placeholder, then it uses appendChild
to move the ad into the placeholder div.
<script type="text/javascript">numAds = 1;for (var i = 0; i < numAds; i++) { source = document.getElementById("adsource-" + i); placeholder = document.getElementById("ad-" + i); placeholder.appendChild(source);}</script>
Consider this entire site a live tech demo. Refresh the page and watch under the “Advertisements” header of the sidebar. After all of the content is loaded, the ads pop into place.
With this setup, the placeholders initially take up no space, so they expand when the ad gets loaded. If this breaks your layout, you can set the height and width of the placeholder to the dimensions of the ad like this:
<div id="ad-0" style="width: 125px; height: 125px"> <!-- this placeholder takes up space! -->
Apparently, this is what I get for writing a post about AdSense. See? This is why we can’t have nice things.