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.

The problem is that the scripts have to be inserted in the body at the place where the advert is desired. This is because Google uses JavaScript's document.write to generate an iframe, which then loads the ad. If Google's servers are being a bit laggy then your visitor has to wait for the Google script to finish loading before the rest of the page is rendered. The closer your ad is to the top of the page, the worse the resulting problem is.

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:

Normal set-up

	Content above advert
	<script type="text/javascript"><!--
		Google ad params
	<script type="text/javascript" src="...show_ads.js"></script>
	Content below advert

Optimised set-up

	Content above advert
	<div id="ad1_inline"></div>
	Content below advert
	<div id="ad1_footer" style="display: none;">
		<script type="text/javascript"><!--
			Google ad params
		<script type="text/javascript" src="...show_ads.js"></script>
	<script type="text/javascript">
		window.onload = function() {
			document.getElementById('ad1_footer').style.display = '';

What it does

  1. Creates a place-holder div for the where the ad will end up
  2. 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
  3. Hides the footer "footer div" with css so the ads don't ever show below your page, causing the page height to "jump"
  4. Waits for the whole page to load before moving the ads (the window.onload bit) thus preventing Internet Explorer from getting really upset.
  5. Moves the ad to it's placeholder, and un-hides it

Of course you may not want to use window.onload. If you attach the function to an event you can modularise the whole ad insertion in your code. Using a JavaScript library (Prototype in this case, but others will look similar) results in the following change to the last script:

	<script type="text/javascript">
		Event.observe(window, 'load', function() {

All scripts are Public Domain.


Perfect, Thanks

Very good post, thanks a lot.

Perfect, works like a charm and the speed of the homepage is so much faster!


Hey thank you for making this,

I would suggest to put in bold the things that are added,
that way we can see clearly what to add and what not to, made a mistake :(

Thanks alot

Thanks to you, travelmastery.com will be a lot faster,
If its works you are going in my Thanks page :)

Thanks a lot!! Now the load speed is faster in quereceta.com !

This is a very detailed and professional solution which helped me to significantly improve the speed of my website sandraandwoo.com today.

Thanks for mentioning this on your site, Novil! I'm using this tweak on kennychronicles.com now, and I've noticed a 1-2 second increase. The biggest benefit will be when Adsense is slower than usual, which it definitely is sometimes.
I've tweeted this to my 68 followers, many of which are other cartoonists with sites. I hope this edit gets spread around more.

Awesome stuff, thank you!

I've just spent an hour or so rewriting all my AdSense calls and the result is my pages are much, much faster.

The pages were fast before I added AdSense, the slow downs it suffered were frustrating the heck out of me so I was on a mission today to bring my pages back up to loading sub two seconds.

You solution was wicked!

Hi, ads do not load i check in firefox error console it shows as security error log. any idea why?

Great Way to do it! Straight and simple!


Thank you - excellent code!

I know this post is old but your code saved my butt wagons. Previously I was using a different iteration of a DIV placeholder that caused errors in Safari and for the life of me I could never figure out a fix, but this version results in no errors across browsers, and seems to work even better. Thanks so much!

Thank you! life saver!

Thank you so much, superb solution, works like a dream. It's now 1.35am but now all sorted thanks to your code. Goodnight!

Awesome solution. thank you for publishing this

Really really good!! It works fine. Very useful on my web cocinadetodo.com
Thanks a lot!!

Thanks for the code, it worked to speed up our site (invisalign-reviews.com) plus we noticed the nice side effect that bots can better read the page. Thanks again!

Great tip, it's always bothered me that my streamline pages loads slowly because of google's ads. My site Alnido Viviendas(alnido.es) has speed up considerably using your code. Thanks.

It is not working in chrome and IE8
Only works in firefox. How do i fix it?

Awesome -- this came in handy. Using this technique, but with jQuery, at Tweet Archivist (tweetarchivist.com).

Thanks for this great implementation! I tried it in my page crete.mmx.gr and I saw great improvement!

Awesome script. It been here for awhile but it is still awesome and I just implemented it on my website!!!