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

greektown casino hampton beach casino ballroom ignition casino 888 casino | https://freeonlinecasino.icu/ - hallmark casino online https://freeonlinecasino.icu/ - rivers casino

myvegas slots free slots slots free play free slots for fun | https://realmoneyslots.icu/ - pop slots casino https://realmoneyslots.icu/ - caesars slots free casino

myvegas slots free slots slots free play free slots for fun | https://realmoneyslots.icu/ - pop slots casino https://realmoneyslots.icu/ - caesars slots free casino

free online slots free online casino slots mgm online casino nj online casino games free | https://slotsonline.ooo/ - casino game https://slotsonline.ooo/ - jack online casino

pch slots tournament online slots real money slot machines free casino slots games | https://vegasslotsonline.icu/ - online slots https://vegasslotsonline.icu/ - jackpot magic slots

belterra casino casino slots empire casino online greektown casino | https://onlinecasinox.icu/ - vegas casino games https://onlinecasinox.icu/ - four winds casino

belterra casino casino slots empire casino online greektown casino | https://onlinecasinox.icu/ - vegas casino games https://onlinecasinox.icu/ - four winds casino

free casino games no download jack online casino turning stone online casino twin river casino | https://casinoo.ooo/ - casinos near me https://casinoo.ooo/ - hollywood casino online slots

free casino games no download jack online casino turning stone online casino twin river casino | https://casinoo.ooo/ - casinos near me https://casinoo.ooo/ - hollywood casino online slots

slotomania free slots free slots no download no registration wizard of oz slots free slot games with no download | https://realmoneyslots.icu/ - play slots online https://realmoneyslots.icu/ - liberty slots

slotomania free slots free slots no download no registration wizard of oz slots free slot games with no download | https://realmoneyslots.icu/ - play slots online https://realmoneyslots.icu/ - liberty slots

free online casino mohegan sun online casino golden nugget online casino lincoln casino | https://freeonlinecasino.icu/ - slotomania slot machines https://freeonlinecasino.icu/ - sugarhouse casino

free online casino mohegan sun online casino golden nugget online casino lincoln casino | https://freeonlinecasino.icu/ - slotomania slot machines https://freeonlinecasino.icu/ - sugarhouse casino

caesars free slots online pop slots casino jackpot magic slots gossip slots | https://vegasslotsonline.icu/ - buffalo slots https://vegasslotsonline.icu/ - slotomania free slots

caesars free slots online pop slots casino jackpot magic slots gossip slots | https://vegasslotsonline.icu/ - buffalo slots https://vegasslotsonline.icu/ - slotomania free slots

empire casino online best online casinos online gambling casino empire city online casino | https://slotsonline.ooo/ - muckleshoot casino https://slotsonline.ooo/ - pala casino

belterra casino jackpot party casino playmgm nj casino online casinos online | https://casinoo.ooo/ - online casino games free https://casinoo.ooo/ - double down casino

doubledown casino free slots free vegas world slots free slots online free vegas slots | https://realmoneyslots.icu/ - free vegas slots online casino https://realmoneyslots.icu/ - free slots no download

online gambling sites casino games free play casino hypercasinos | https://freeonlinecasino.icu/ - free vegas casino games https://freeonlinecasino.icu/ - empire city online casino

online gambling sites casino games free play casino hypercasinos | https://freeonlinecasino.icu/ - free vegas casino games https://freeonlinecasino.icu/ - empire city online casino

vegas casino slots igt free slots caesars slots free casino konami free slots | https://vegasslotsonline.icu/ - gossip slots https://vegasslotsonline.icu/ - slots free online

paradise casino turning stone online casino bonus casino free vegas casino games | https://slotsonline.ooo/ - high 5 casino https://slotsonline.ooo/ - snoqualmie casino

double down casino vegas casino online real casino grand falls casino | https://casinoo.ooo/ - hallmark casino online https://casinoo.ooo/ - free casino games no download

double down casino vegas casino online real casino grand falls casino | https://casinoo.ooo/ - hallmark casino online https://casinoo.ooo/ - free casino games no download

free online slots free video slots vegas slots online free jackpot party casino slots | https://vegasslotsonline.icu/ - free online slots games https://vegasslotsonline.icu/ - vegas free slots online

empire city casino dakota sioux casino thunder valley casino grand casino hinckley | https://onlinecasinox.icu/ - treasure island casino minnesota https://onlinecasinox.icu/ - free online slots

empire city casino dakota sioux casino thunder valley casino grand casino hinckley | https://onlinecasinox.icu/ - treasure island casino minnesota https://onlinecasinox.icu/ - free online slots

lady luck online casino virgin casino online play casino clearwater casino | https://slotsonline.ooo/ - online casino slots https://slotsonline.ooo/ - free casino games online

mystic lake casino firekeepers casino muckleshoot casino online casino real money | https://casinoo.ooo/ - doubledown casino facebook https://casinoo.ooo/ - del lago casino

mystic lake casino firekeepers casino muckleshoot casino online casino real money | https://casinoo.ooo/ - doubledown casino facebook https://casinoo.ooo/ - del lago casino

free slot games with no download slots lounge doubledown casino free slots free slots hollywood | https://vegasslotsonline.icu/ - online casino slots no download https://vegasslotsonline.icu/ - turning stone online slots

free slot games with no download slots lounge doubledown casino free slots free slots hollywood | https://vegasslotsonline.icu/ - online casino slots no download https://vegasslotsonline.icu/ - turning stone online slots

turning stone casino online casinos for us players casino slot casinos near me | https://casinoo.ooo/ - chinook winds casino https://casinoo.ooo/ - doubledown casino facebook

turning stone casino online casinos for us players casino slot casinos near me | https://casinoo.ooo/ - chinook winds casino https://casinoo.ooo/ - doubledown casino facebook

san manuel casino chinook winds casino hollywood casino online foxwoods casino online | https://onlinecasinox.icu/ - fortune bay casino https://onlinecasinox.icu/ - san manuel online casino

san manuel casino chinook winds casino hollywood casino online foxwoods casino online | https://onlinecasinox.icu/ - fortune bay casino https://onlinecasinox.icu/ - san manuel online casino

free casino games sun moon real casino slots jackpot party casino facebook pala casino online nj | https://slotsonline.ooo/ - casino real money https://slotsonline.ooo/ - gun lake casino

free casino games sun moon real casino slots jackpot party casino facebook pala casino online nj | https://slotsonline.ooo/ - casino real money https://slotsonline.ooo/ - gun lake casino

world class casino slots house of fun slots free slots games online slot games | https://vegasslotsonline.icu/ - heart of vegas free slots https://vegasslotsonline.icu/ - slotomania free slots

world class casino slots house of fun slots free slots games online slot games | https://vegasslotsonline.icu/ - heart of vegas free slots https://vegasslotsonline.icu/ - slotomania free slots

parx casino online free vegas casino games vegas casino games online gambling sites | https://casinoo.ooo/ - virgin online casino https://casinoo.ooo/ - posh casino

parx casino online free vegas casino games vegas casino games online gambling sites | https://casinoo.ooo/ - virgin online casino https://casinoo.ooo/ - posh casino

free online casino foxwoods casino online tropicana online casino gsn casino games | https://onlinecasinox.icu/ - online gambling sites https://onlinecasinox.icu/ - hollywood online casino

free online casino foxwoods casino online tropicana online casino gsn casino games | https://onlinecasinox.icu/ - online gambling sites https://onlinecasinox.icu/ - hollywood online casino

snoqualmie casino borgata online casino high 5 casino bigfish casino online games | https://freeonlinecasino.icu/ - casino near me https://freeonlinecasino.icu/ - empire city casino

snoqualmie casino borgata online casino high 5 casino bigfish casino online games | https://freeonlinecasino.icu/ - casino near me https://freeonlinecasino.icu/ - empire city casino

Pages

Add new comment