I have just found a way to attach the AdSense JavaScript to the bottom of the web page (where all the JavaScript belongs) and have it insert the ad in the right place. I had been looking for a solution to this for several years whenever someone reminded me about the stupid problem with Gooogle's code (still written with Netscape 3 in mind because of the document.write statements).
Step One - Go to https://github.com/krux/postscribe and get the postscribe.js and htmlparser.js files.
Step Two. Create a separate ad99.js file for each ad with the ad code in it and using postscribe to add the external script from Google.
Code:
google_ad_client = "pub-9999999999999999";
google_ad_slot = "9999999999";
google_ad_width = 300;
google_ad_height = 250;
postscribe('#adv1','<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"><\/script>');
Step Three. Add a div with the specified id (id="adv1" in my example) where you want the ad to appear.
Step Four. Repeat steps two and three for any additional ads - each needs to be in a separate file as the postscribe call must be the last line.
Step Five. Add the following to the bottom of your page immediately before the </body> tag - listing each of the ad scripts you created in place of ad1.js and ad2.js (update the src to include the path to where ever you put the scripts).
Code:
<script type="text/javascript" src="postscribe.js"></script>
<script type="text/javascript" src="htmlparser.js"></script>
<script type="text/javascript" src="ad1.js"></script>
<script type="text/javascript" src="ad2.js"></script>
I believe that the htmlparser script originated with John Resig who also created JQuery. I came across a version of that code with his name on it a couple of years ago but was never able to figure out how to get it to place Google ads in the right place (I could get the script tags for the ad inserted at the right spot but the ad still appeared after the script that added those tags rather than in that spot. Those responsible for postscribe appear to have resolved that problem so that the ad itself will also appear where it is supposed to.
Bookmarks