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

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

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

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

online gambling casino best online casino mgm online casino | [url=https://onlinecasinowus.com/]gsn casino slots[/url]

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

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

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

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

online casinos bovada casino online casino games [url=https://casinoslotssy.us.org/]real money casino[/url]

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

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

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

free online casino slots bovada casino play casino [url=https://casinoslotsy.us.org/]gambling sites[/url]

free online casino slots bovada casino play casino [url=https://casinoslotsy.us.org/]gambling sites[/url]

mgm online casino online casinos for us players free online casino slots | [url=https://onlinecasinovus.com/]online casino gambling[/url]

best online casinos online casino games gsn casino games [url=https://onlinecasinoster.us.org/]bovada casino[/url]

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

free online casino slots casino play gambling sites [url=https://onlinecasinoslotsparty.com/]usa online casino[/url]

free online casino slots casino play gambling sites [url=https://onlinecasinoslotsparty.com/]usa online casino[/url]

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

online casino slots online casino games online casinos for us players [url=https://onlinecasinoster.us.org/]betfair online casino[/url]

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

usa online casino casino bonus usa online casino [url=https://onlinecasinoslotsparty.com/]play casino[/url]

online casino slots casino play best online casino [url=https://onlinecasinolo.com/]bovada casino[/url]

buy viagra online no new posts
[url=http://viagrawithoutdoctorpres.net]viagra without prescription
[/url] viagra prices uk
viagra without a doctor prescription canada
- viagra 20mg side effects posts per day
other health benefits of viagra

betfair online casino gsn casino slots gsn casino slots [url=https://onlinecasinog24.com/]best online casinos[/url]

casino blackjack online casino games usa online casino [url=https://onlinecasinolo.com/]bovada casino[/url]

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

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

online gambling casino online gambling gsn casino [url=https://onlinecasinog24.com/]casino blackjack[/url]

casino slots slots for real money slots for real money [url=https://onlinecasinolo.com/]casino games[/url]

online casinos casino blackjack online casino [url=https://onlinecasinolc.com/]online gambling[/url]

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

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

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

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

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

casino blackjack casino play real money casino | [url=https://onlinecasinoppl.com/]online gambling[/url]

casino blackjack casino play real money casino | [url=https://onlinecasinoppl.com/]online gambling[/url]

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

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

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

online gambling casino best online casino online casino games [url=https://onlinecasinorealmoney.us.org/]online casino games[/url]

online gambling casino best online casino online casino games [url=https://onlinecasinorealmoney.us.org/]online casino games[/url]

casino play online casinos for us players free online casino slots | [url=https://onlinecasinoppl.com/]best online casinos[/url]

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

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

gambling sites gsn casino games mgm online casino [url=https://onlinecasinoslotsnow.com/]mgm online casino[/url]

gambling sites gsn casino games mgm online casino [url=https://onlinecasinoslotsnow.com/]mgm online casino[/url]

online casino gambling casino online best online casino [url=https://onlinecasinoslotsparty.com/]betfair online casino[/url]

Pages

Add new comment