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

ultram 50 mg tramadol opioid tramadol 50mg | [url=https://buytramadol50mg.us.org/]buy tramadol[/url]

ultram 50 mg tramadol opioid tramadol 50mg | [url=https://buytramadol50mg.us.org/]buy tramadol[/url]

online casinos online gambling online casino slots | [url=https://online-casinoss.us.org/]casino bonus[/url]

online casinos online gambling online casino slots | [url=https://online-casinoss.us.org/]casino bonus[/url]

tramadol hcl tramadol hcl 50 mg tablet tramadol withdrawal | [url=https://buytramadol50mg.us.org/]ultram 50 mg[/url]

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

painkillers online with next day delivery tramadol 50 mg tablet buy tramadol | [url=https://buytramadol50mg.us.org/]buy tramadol[/url]

painkillers online with next day delivery tramadol 50 mg tablet buy tramadol | [url=https://buytramadol50mg.us.org/]buy tramadol[/url]

mgm online casino online casinos free online casino slots [url=https://onlinecasinoslotsnow.com/]online casino gambling[/url]

casino online jupiters casino online casino | [url=https://au-onlinecasinogames.com/]star casino[/url]

casino online jupiters casino online casino | [url=https://au-onlinecasinogames.com/]star casino[/url]

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

betfair online casino usa online casino mgm online casino | [url=https://onlinecasinoppl.com/]online casinos[/url]

betfair online casino usa online casino mgm online casino | [url=https://onlinecasinoppl.com/]online casinos[/url]

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

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

mgm online casino slots for real money online casinos [url=https://onlinecasinoslotsnow.com/]usa online casino[/url]

cashman casino rich casino crown casino perth | [url=https://au-onlinecasinogames.com/]crown casino[/url]

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

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

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

online casino games online casino bonus casino bonus [url=https://onlinecasinoslotsparty.com/]best online casinos[/url]

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

casino slots free online casino slots casino slots [url=https://onlinecasinoslotsnow.com/]real money casino[/url]

casino slots free online casino slots casino slots [url=https://onlinecasinoslotsnow.com/]real money casino[/url]

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

tangiers casino star casino joe fortune casino | [url=https://au-onlinecasinogames.com/]star casino sydney[/url]

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

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

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

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

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

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

online casino doubledown casino treasury casino | [url=https://au-onlinecasinogames.com/]online casino[/url]

online casino doubledown casino treasury casino | [url=https://au-onlinecasinogames.com/]online casino[/url]

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

casino slots mgm online casino online casinos [url=https://onlinecasinoslotsnow.com/]best online casinos[/url]

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

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

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

aol games casino treasury casino online casino | [url=https://au-onlinecasinogames.com/]star casino sydney[/url]

aol games casino treasury casino online casino | [url=https://au-onlinecasinogames.com/]star casino sydney[/url]

casino games casino bonus real money casino | [url=https://onlinecasinomaniaa.com/]casino bonus[/url]

casino games casino bonus real money casino | [url=https://onlinecasinomaniaa.com/]casino bonus[/url]

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

online gambling best online casinos casino slots [url=https://onlinecasinoslotsnow.com/]casino bonus[/url]

online gambling best online casinos casino slots [url=https://onlinecasinoslotsnow.com/]casino bonus[/url]

gsn casino online casinos online casino bonus [url=https://onlinecasinogamesnt.us.org/]usa online casino[/url]

gsn casino online casinos online casino bonus [url=https://onlinecasinogamesnt.us.org/]usa online casino[/url]

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

Pages

Add new comment