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

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

Optimised set-up

<body>
	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>
		<script type="text/javascript" src="...show_ads.js"></script>
	</div>
	<script type="text/javascript">
		window.onload = function() {
			document.getElementById('ad1_inline').appendChild(document.getElementById('ad1_footer'));
			document.getElementById('ad1_footer').style.display = '';
		}
	</script>
</body>

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() {
			$('ad1_inline').appendChild($('ad1_footer'));
			$('ad1_footer').show();
		}
	</script>
	...

All scripts are Public Domain.

Comments

free online casino slots usa online casino online gambling casino [url=https://onlinecasinogamesnt.us.org/]online casino gambling[/url]

free online casino slots usa online casino online gambling casino [url=https://onlinecasinogamesnt.us.org/]online casino gambling[/url]

casino play casino games gsn casino games | [url=https://onlinecasinovus.com/]online gambling[/url]

casino play casino games gsn casino games | [url=https://onlinecasinovus.com/]online gambling[/url]

online casino real money casino online usa online casino [url=https://onlinecasinogamestt.us.org/]online casino games[/url]

betfair online casino online casino games mgm online casino [url=https://onlinecasinolo.com/]online casinos for us players[/url]

online gambling real money casino online casino slots [url=https://onlinecasinogamesnt.us.org/]bovada casino[/url]

online gambling real money casino online casino slots [url=https://onlinecasinogamesnt.us.org/]bovada casino[/url]

online gambling online gambling casino casino bonus [url=https://onlinecasinogamestt.us.org/]online casinos for us players[/url]

online gambling online gambling casino casino bonus [url=https://onlinecasinogamestt.us.org/]online casinos for us players[/url]

best online casinos casino real money gambling sites [url=https://onlinecasinogamesnt.us.org/]gambling sites[/url]

best online casinos casino real money gambling sites [url=https://onlinecasinogamesnt.us.org/]gambling sites[/url]

best online casinos casino online online casino bonus [url=https://onlinecasinogamestt.us.org/]gambling sites[/url]

best online casinos casino online online casino bonus [url=https://onlinecasinogamestt.us.org/]gambling sites[/url]

online gambling casino slots free online casino slots [url=https://onlinecasinogamesnt.us.org/]online casinos for us players[/url]

online casino real money online casino gambling free online casino slots [url=https://onlinecasinogamestt.us.org/]online casino games[/url]

online casino real money online casino gambling free online casino slots [url=https://onlinecasinogamestt.us.org/]online casino games[/url]

online casino games online casino casino blackjack | [url=https://onlinecasinovus.com/]casino online[/url]

online casino games online casino casino blackjack | [url=https://onlinecasinovus.com/]casino online[/url]

mgm online casino gambling sites casino games [url=https://onlinecasinogamesnt.us.org/]play casino[/url]

slots for real money usa online casino online gambling | [url=https://onlinecasinovus.com/]online casinos[/url]

online gambling online casino games casino bonus [url=https://onlinecasinogamestt.us.org/]gsn casino games[/url]

online gambling online casino games casino bonus [url=https://onlinecasinogamestt.us.org/]gsn casino games[/url]

online casino games online casino slots real money casino [url=https://onlinecasinogamesnt.us.org/]usa online casino[/url]

online casino games online casino slots real money casino [url=https://onlinecasinogamesnt.us.org/]usa online casino[/url]

online casino games free online casino slots casino online [url=https://onlinecasinogamestt.us.org/]bovada casino[/url]

best online casino bovada casino real money casino | [url=https://onlinecasinovus.com/]bovada casino[/url]

best online casino bovada casino real money casino | [url=https://onlinecasinovus.com/]bovada casino[/url]

usa online casino casino bonus online casino gambling [url=https://onlinecasinogamesnt.us.org/]bovada casino[/url]

casino blackjack casino play casino blackjack [url=https://onlinecasinogamestt.us.org/]online casinos[/url]

casino blackjack casino play casino blackjack [url=https://onlinecasinogamestt.us.org/]online casinos[/url]

online casinos online casino bonus casino play | [url=https://onlinecasinovus.com/]best online casinos[/url]

online casinos online casino bonus casino play | [url=https://onlinecasinovus.com/]best online casinos[/url]

online casino slots online casino bonus casino games [url=https://onlinecasinogamesnt.us.org/]casino bonus[/url]

online casino slots online casino bonus casino games [url=https://onlinecasinogamesnt.us.org/]casino bonus[/url]

slots for real money usa online casino online casino gambling [url=https://onlinecasinogamestt.us.org/]online gambling[/url]

casino online online casinos slots for real money [url=https://onlinecasinogamesnt.us.org/]casino games[/url]

casino online online casinos for us players usa online casino | [url=https://onlinecasinovus.com/]gsn casino games[/url]

casino online mgm online casino casino slots [url=https://onlinecasinolc.com/]casino bonus[/url]

casino bonus casino slots play casino [url=https://casinoslotssy.us.org/]casino slots[/url]

slots for real money casino slots real money casino [url=https://casinoslotsy.us.org/]online casino games[/url]

gambling sites online casino games gsn casino slots [url=https://onlinecasinormoney.com/]slots for real money[/url]

online casino real money gsn casino online casino bonus | [url=https://onlinecasinovus.com/]play casino[/url]

online casino slots for real money mgm online casino [url=https://casinoslotssy.us.org/]online casinos[/url]

online casino slots for real money mgm online casino [url=https://casinoslotssy.us.org/]online casinos[/url]

slots for real money online casino casino bonus [url=https://onlinecasinormoney.com/]best online casinos[/url]

slots for real money online casino casino bonus [url=https://onlinecasinormoney.com/]best online casinos[/url]

online casino games online casino games casino play | [url=https://onlinecasinovus.com/]play casino[/url]

real money casino bovada casino casino bonus [url=https://casinoslotsy.us.org/]online casino slots[/url]

real money casino bovada casino casino bonus [url=https://casinoslotsy.us.org/]online casino slots[/url]

Pages

Add new comment