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

casinos near me hollywood casino play4fun chumash casino high 5 casino | https://casino-bonus.icu/ - casino games free online https://casino-bonus.icu/ - casino bonus

vegas world free slots games free slots games online free online slots games best online slots | https://playslotsonline.icu/ - free slots casino games https://playslotsonline.icu/ - house of fun free slots

muckleshoot casino mountaineer casino usa online casino gun lake casino | https://playcasinogames.icu/ - tropicana online casino https://playcasinogames.icu/ - casino blackjack

muckleshoot casino mountaineer casino usa online casino gun lake casino | https://playcasinogames.icu/ - tropicana online casino https://playcasinogames.icu/ - casino blackjack

hollywood casino hollywood online casino slots casino games las vegas casinos | https://playslots.ooo/ - play free vegas casino games https://playslots.ooo/ - casinos online

hollywood casino hollywood online casino slots casino games las vegas casinos | https://playslots.ooo/ - play free vegas casino games https://playslots.ooo/ - casinos online

bovada blackjack paradise casino online casino games free hollywood casino online | https://casinoplay.icu/ - parx online casino https://casinoplay.icu/ - grand casino hinckley

free slots 777 pompeii slots konami free slots vegas slots online | https://playslotsonline.icu/ - pop slots casino https://playslotsonline.icu/ - slot games free

free slots 777 pompeii slots konami free slots vegas slots online | https://playslotsonline.icu/ - pop slots casino https://playslotsonline.icu/ - slot games free

golden nugget online casino casino games free free casino games sun moon hollywood casino | https://onlinecasinorealmoney.ooo/ - gsn casino https://onlinecasinorealmoney.ooo/ - huuuge casino

golden nugget online casino casino games free free casino games sun moon hollywood casino | https://onlinecasinorealmoney.ooo/ - gsn casino https://onlinecasinorealmoney.ooo/ - huuuge casino

free penny slots igt free slots free online slots vegas world vegas world free games online slots | https://freecasinoslots.icu/ - scatter slots https://freecasinoslots.icu/ - free slots hollywood

real casino empire city casino oxford casino bovada casino | https://casinovegas.ooo/ - online casino games free https://casinovegas.ooo/ - slotomania slot machines

online gambling thunder valley casino playmgm nj casino online rivers casino schenectady | https://vegascasino.icu/ - snoqualmie casino https://vegascasino.icu/ - lincoln casino

online gambling thunder valley casino playmgm nj casino online rivers casino schenectady | https://vegascasino.icu/ - snoqualmie casino https://vegascasino.icu/ - lincoln casino

four winds casino best online casinos fortune bay casino empire city casino | https://freeonlineslots.ooo/ - online casinos https://freeonlineslots.ooo/ - free vegas slots online casino

free online casino slots play free slots for fun free buffalo slots free slots games | https://casinoslotsonline.icu/ - slots online https://casinoslotsonline.icu/ - free slots vegas

free online casino slots play free slots for fun free buffalo slots free slots games | https://casinoslotsonline.icu/ - slots online https://casinoslotsonline.icu/ - free slots vegas

best online casinos best online casinos play casino | https://onlinecasino888.us.org/ - free slots casino games

zone online casino four winds casino casino near me free online slots | https://casinovegas.ooo/ - jackpot party casino https://casinovegas.ooo/ - high five casino slots

wheel of fortune slots my vegas slots slots online free vegas free slots | https://casinoslotsonline.icu/ - quick hit slots https://casinoslotsonline.icu/ - caesars free slots online

gsn casino slots online gambling sites vegas world casino games play online casino | https://freeonlineslots.ooo/ - mgm online casino nj https://freeonlineslots.ooo/ - playmgm nj casino online

myvegas slots free slot games slot machines vegas slots free | https://freecasinoslots.icu/ - penny slots https://freecasinoslots.icu/ - free casino slots games

myvegas slots free slot games slot machines vegas slots free | https://freecasinoslots.icu/ - penny slots https://freecasinoslots.icu/ - free casino slots games

online casino games free online casino reviews harrah online casino online casino games free | https://vegascasino.icu/ - sugarhouse online casino https://vegascasino.icu/ - casino games

online casino games free online casino reviews harrah online casino online casino games free | https://vegascasino.icu/ - sugarhouse online casino https://vegascasino.icu/ - casino games

free online casino slots real money casino lady luck online casino | https://onlinecasino888.us.org/ - casino blackjack

free online casino slots real money casino lady luck online casino | https://onlinecasino888.us.org/ - casino blackjack

free casino games no download pechanga casino sugarhouse casino online hollywood casino online slots | [url=https://casinoplay.icu/]free casino[/url] [url=https://casinoplay.icu/]slots for real money[/url]

free casino games no download pechanga casino sugarhouse casino online hollywood casino online slots | [url=https://casinoplay.icu/]free casino[/url] [url=https://casinoplay.icu/]slots for real money[/url]

slots farm casino slots free games free slots casino games casino slots | https://casinoslotsonline.icu/ - slotomania on facebook https://casinoslotsonline.icu/ - casino slots free games

free casino clearwater casino posh casino casino games slots free | https://casinovegas.ooo/ - mgm online casino https://casinovegas.ooo/ - chumba casino

usa online casino pala casino online nj prairie meadows casino choctaw casino durant oklahoma | https://freeonlineslots.ooo/ - free casino games slots https://freeonlineslots.ooo/ - free slots casino games

free online slots games house of fun free slots caesars slots hollywood casino online slots free | https://freecasinoslots.icu/ - free online slots https://freecasinoslots.icu/ - gsn casino slots

free online slots games house of fun free slots caesars slots hollywood casino online slots free | https://freecasinoslots.icu/ - free online slots https://freecasinoslots.icu/ - gsn casino slots

paradise casino online casinos hollywood online casino free casino games slot machines | https://casinoplay.icu/ - casino games https://casinoplay.icu/ - belterra casino

casino game pala casino online stn play online casino belterra casino | https://vegascasino.icu/ - free casino https://vegascasino.icu/ - slots for real money

casino game pala casino online stn play online casino belterra casino | https://vegascasino.icu/ - free casino https://vegascasino.icu/ - slots for real money

best online casino casino play casino real money | https://onlinecasino888.us.org/ - caesars online casino

best online casino casino play casino real money | https://onlinecasino888.us.org/ - caesars online casino

liberty slots casino free online slots no download free slot games online free online casino slots | https://casinoslotsonline.icu/ - quick hits free slots https://casinoslotsonline.icu/ - world class casino slots

spirit mountain casino winstar world casino mgm online casino lady luck online casino | https://casinovegas.ooo/ - stn play online casino https://casinovegas.ooo/ - ignition casino

spirit mountain casino winstar world casino mgm online casino lady luck online casino | https://casinovegas.ooo/ - stn play online casino https://casinovegas.ooo/ - ignition casino

old vegas slots house of fun slots heart of vegas free slots free penny slots no download | https://freecasinoslots.icu/ - gamepoint slots https://freecasinoslots.icu/ - slots for free

old vegas slots house of fun slots heart of vegas free slots free penny slots no download | https://freecasinoslots.icu/ - gamepoint slots https://freecasinoslots.icu/ - slots for free

sugarhouse online casino red rock casino online casinos free vegas casino games | https://freeonlineslots.ooo/ - free vegas slots online casino https://freeonlineslots.ooo/ - chinook winds casino

casino game oxford casino vegas world casino games casinos near me | https://casinoplay.icu/ - seneca niagara casino https://casinoplay.icu/ - gsn casino games

casino game oxford casino vegas world casino games casinos near me | https://casinoplay.icu/ - seneca niagara casino https://casinoplay.icu/ - gsn casino games

online casino games seneca allegany casino vegas casino online blue chip casino | https://vegascasino.icu/ - pala casino online https://vegascasino.icu/ - soaring eagle casino

online casino games seneca allegany casino vegas casino online blue chip casino | https://vegascasino.icu/ - pala casino online https://vegascasino.icu/ - soaring eagle casino

Pages

Add new comment