Speed up page load times when running Google ads
Google ads are an easy way to monetize your site, but their implementation leaves a lot to be desired. To insert a Google ad block on a page one has to insert two script blocks: one to set the adverts parameters, and one to load the external Google script.
A number of solutions have been proposed, including overwriting the document.write method, and inserting the Google script through DOM manipulation. Unfortunately this breaks in Safari 2.0, and breaks any other instances of document.write you may be using.
So here is the solution we've come up with:
What it does
- Creates a place-holder div for the where the ad will end up
- Places all the ad code in an "footer div" at the bottom of the page - this prevents your page from "locking" while the Google scripts load
- Hides the footer "footer div" with css so the ads don't ever show below your page, causing the page height to "jump"
- Waits for the whole page to load before moving the ads (the window.onload bit) thus preventing Internet Explorer from getting really upset.
- Moves the ad to it's placeholder, and un-hides it
All scripts are Public Domain.