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

gsn casino games gsn casino gsn casino slots [url=https://casinoslotssy.us.org/]online casino gambling[/url]

casino games gambling sites online casinos [url=https://casinoslotsy.us.org/]online gambling casino[/url]

casino real money mgm online casino online casinos for us players | [url=https://onlinecasinozonee.com/]play casino[/url]

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

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

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

gsn casino bovada casino online gambling casino [url=https://onlinecasinormoney.com/]gsn casino slots[/url]

gsn casino bovada casino online gambling casino [url=https://onlinecasinormoney.com/]gsn casino slots[/url]

casino games online casino real money online casino [url=https://onlinecasinolc.com/]online casino games[/url]

casino games online casino real money online casino [url=https://onlinecasinolc.com/]online casino games[/url]

casino bonus best online casino gsn casino slots | [url=https://onlinecasinozonee.com/]gsn casino slots[/url]

casino bonus best online casino gsn casino slots | [url=https://onlinecasinozonee.com/]gsn casino slots[/url]

play casino mgm online casino online casinos [url=https://onlinecasinogamesnt.us.org/]mgm online casino[/url]

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

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

online casino slots casino slots online casino bonus [url=https://onlinecasinormoney.com/]online casinos[/url]

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

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

mgm online casino play casino gsn casino games [url=https://onlinecasinogamestt.us.org/]best online casino[/url]

online casino real money free online casino slots play casino [url=https://onlinecasinogamesnt.us.org/]best online casinos[/url]

casino slots gsn casino slots best online casinos [url=https://onlinecasinolc.com/]online casino games[/url]

casino slots gsn casino slots best online casinos [url=https://onlinecasinolc.com/]online casino games[/url]

online casino real money casino online casino blackjack [url=https://onlinecasinormoney.com/]casino bonus[/url]

online casino real money casino online casino blackjack [url=https://onlinecasinormoney.com/]casino bonus[/url]

free online casino slots casino blackjack casino bonus | [url=https://onlinecasinozonee.com/]online casino games[/url]

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

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

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

real money casino casino bonus casino games [url=https://onlinecasinormoney.com/]casino slots[/url]

casino games casino play online casino gambling [url=https://onlinecasinolc.com/]casino slots[/url]

online gambling casino casino online online casino real money | [url=https://onlinecasinozonee.com/]casino games[/url]

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

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

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

casino bonus play casino best online casino [url=https://onlinecasinormoney.com/]free online casino slots[/url]

gsn casino casino slots casino games [url=https://onlinecasinolc.com/]online casino gambling[/url]

free slot machine games - [url=https://slots777usa.com/]slotomania free slots[/url]
https://slots777usa.com/

free slot machine games - [url=https://slots777usa.com/]slotomania free slots[/url]
https://slots777usa.com/

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

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

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

online gambling casino slots bovada casino [url=https://onlinecasinormoney.com/]casino play[/url]

online gambling casino slots bovada casino [url=https://onlinecasinormoney.com/]casino play[/url]

usa online casino best online casino online casino [url=https://onlinecasinolc.com/]online casino games[/url]

usa online casino best online casino online casino [url=https://onlinecasinolc.com/]online casino games[/url]

casino bonus gsn casino slots casino play | [url=https://onlinecasinozonee.com/]online casino games[/url]

casino games online casinos online gambling | [url=https://onlinecasinozonee.com/]mgm online casino[/url]

casino games online casinos online gambling | [url=https://onlinecasinozonee.com/]mgm online casino[/url]

online casino games casino slots online casino | [url=https://onlinecasinozonee.com/]casino online[/url]

online casino games casino slots online casino | [url=https://onlinecasinozonee.com/]casino online[/url]

Pages

Add new comment