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

pch slots online casino slots slotomania free slots hollywood casino free slot play | https://slotgames.icu/ - hollywood casino free slot play https://slotgames.icu/ - da vinci diamonds free online slots

hollywood casino online slots online casino slots foxwoods casino online slots gsn casino slots | https://playonlinecasino.icu/ - firekeepers casino https://playonlinecasino.icu/ - foxwoods casino online

vegas world casino games online casinos for us players free casino games hallmark casino online | https://onlinecasinoo.icu/ - casino bonus https://onlinecasinoo.icu/ - gsn casino games

vegas world casino games online casinos for us players free casino games hallmark casino online | https://onlinecasinoo.icu/ - casino bonus https://onlinecasinoo.icu/ - gsn casino games

big fish casino slots bovada blackjack free casino games slots pala casino | https://playcasinoslots.ooo/ - rivers casino https://playcasinoslots.ooo/ - san manuel online casino

big fish casino slots bovada blackjack free casino games slots pala casino | https://playcasinoslots.ooo/ - rivers casino https://playcasinoslots.ooo/ - san manuel online casino

wizard of oz slots liberty slots casino free online slots no download no registration vegas free slots online | https://slotsrealmoney.icu/ - online casino slots no download https://slotsrealmoney.icu/ - jackpot magic slots

wizard of oz slots liberty slots casino free online slots no download no registration vegas free slots online | https://slotsrealmoney.icu/ - online casino slots no download https://slotsrealmoney.icu/ - jackpot magic slots

casino games free gsn casino free slots games free casino games vegas world | https://onlinecasinoplay.us.org/ - caesars online casino https://onlinecasinoplay.us.org/ - online gambling casino

virgin online casino vegas casino online play free vegas casino games doubledown casino facebook | https://realmoneycasino.ooo/ - foxwoods casino https://realmoneycasino.ooo/ - no deposit casino

virgin online casino vegas casino online play free vegas casino games doubledown casino facebook | https://realmoneycasino.ooo/ - foxwoods casino https://realmoneycasino.ooo/ - no deposit casino

free buffalo slots liberty slots monopoly slots slots free games | https://slotgames.icu/ - online casino slots https://slotgames.icu/ - slots free

oxford casino casino games slots free slots for real money winstar casino | https://playonlinecasino.icu/ - tropicana online casino https://playonlinecasino.icu/ - san manuel online casino

free casino games slots online casino bonus mohegan sun online casino red rock casino | https://onlinecasinoo.icu/ - online casino reviews https://onlinecasinoo.icu/ - jackpot party casino facebook

free penny slots free penny slots online online slot games online slot games | https://slotsrealmoney.icu/ - free casino slots games https://slotsrealmoney.icu/ - buffalo gold slots

world class casino slots free slots online free vegas slots buffalo gold slots | https://slotgames.icu/ - brian christopher slots https://slotgames.icu/ - free online slots

world class casino slots free slots online free vegas slots buffalo gold slots | https://slotgames.icu/ - brian christopher slots https://slotgames.icu/ - free online slots

caesars casino prairie meadows casino online casino slots play casino | https://playcasinoslots.ooo/ - belterra casino https://playcasinoslots.ooo/ - turning stone online casino

caesars casino prairie meadows casino online casino slots play casino | https://playcasinoslots.ooo/ - belterra casino https://playcasinoslots.ooo/ - turning stone online casino

winstar casino oklahoma soaring eagle casino doubledown casino facebook rivers casino | https://realmoneycasino.ooo/ - oxford casino https://realmoneycasino.ooo/ - casino near me

winstar casino oklahoma soaring eagle casino doubledown casino facebook rivers casino | https://realmoneycasino.ooo/ - oxford casino https://realmoneycasino.ooo/ - casino near me

double down casino casino online slots hypercasinos free casino | https://onlinecasinoo.icu/ - casino online slots https://onlinecasinoo.icu/ - pala casino

slots for real money online gambling big fish casino free slots casino games | [url=https://onlinecasinoplay.us.org/]vegas world casino games[/url] [url=https://onlinecasinoplay.us.org/]free online slots[/url]

slots for real money online gambling big fish casino free slots casino games | [url=https://onlinecasinoplay.us.org/]vegas world casino games[/url] [url=https://onlinecasinoplay.us.org/]free online slots[/url]

golden nugget online casino casinos near me casino games slots free gambling sites | https://playonlinecasino.icu/ - winstar casino https://playonlinecasino.icu/ - lady luck

golden nugget online casino casinos near me casino games slots free gambling sites | https://playonlinecasino.icu/ - winstar casino https://playonlinecasino.icu/ - lady luck

casino games slots free online gambling casino games free online casino game | https://realmoneycasino.ooo/ - cherokee casino https://realmoneycasino.ooo/ - free casino games slotomania

slotomania on facebook free games for casino slots hollywood free slots vegas las vegas free slots | https://slotgames.icu/ - free slots with no download or registration https://slotgames.icu/ - free casino slots no download

slotomania on facebook free games for casino slots hollywood free slots vegas las vegas free slots | https://slotgames.icu/ - free slots with no download or registration https://slotgames.icu/ - free casino slots no download

seneca allegany casino chumash casino online casino real money free online casino games | https://playcasinoslots.ooo/ - casino games free https://playcasinoslots.ooo/ - zone online casino

seneca allegany casino chumash casino online casino real money free online casino games | https://playcasinoslots.ooo/ - casino games free https://playcasinoslots.ooo/ - zone online casino

pop slots pompeii slots slots games heart of vegas slots | https://slotsrealmoney.icu/ - konami free slots https://slotsrealmoney.icu/ - penny slots

pop slots pompeii slots slots games heart of vegas slots | https://slotsrealmoney.icu/ - konami free slots https://slotsrealmoney.icu/ - penny slots

pala casino online nj gsn casino slots gsn casino games vegas casino online | https://onlinecasinoo.icu/ - play online casino https://onlinecasinoo.icu/ - jack online casino

pala casino online nj gsn casino slots gsn casino games vegas casino online | https://onlinecasinoo.icu/ - play online casino https://onlinecasinoo.icu/ - jack online casino

maryland live casino online choctaw casino durant oklahoma gsn casino sugarhouse online casino | https://playcasinoslots.ooo/ - foxwoods casino https://playcasinoslots.ooo/ - slots casino games

maryland live casino online choctaw casino durant oklahoma gsn casino sugarhouse online casino | https://playcasinoslots.ooo/ - foxwoods casino https://playcasinoslots.ooo/ - slots casino games

charlestown races and slots pch slots tournament free slot games free buffalo slots | https://slotsrealmoney.icu/ - slot machines https://slotsrealmoney.icu/ - simslots free slots

charlestown races and slots pch slots tournament free slot games free buffalo slots | https://slotsrealmoney.icu/ - slot machines https://slotsrealmoney.icu/ - simslots free slots

liberty slots slots games monopoly slots buffalo gold slots | https://slotgames.icu/ - free buffalo slots https://slotgames.icu/ - free slots online

liberty slots slots games monopoly slots buffalo gold slots | https://slotgames.icu/ - free buffalo slots https://slotgames.icu/ - free slots online

huuuge casino free online casino slots maryland live casino online betfair casino online nj | https://realmoneycasino.ooo/ - free slots games https://realmoneycasino.ooo/ - rivers casino schenectady

huuuge casino free online casino slots maryland live casino online betfair casino online nj | https://realmoneycasino.ooo/ - free slots games https://realmoneycasino.ooo/ - rivers casino schenectady

casino games free online mgm online casino firekeepers casino zone online casino | https://onlinecasinoplay.us.org/ - online casino slots https://onlinecasinoplay.us.org/ - free vegas casino games

casino games free online mgm online casino firekeepers casino zone online casino | https://onlinecasinoplay.us.org/ - online casino slots https://onlinecasinoplay.us.org/ - free vegas casino games

real money casino mohegan sun casino san manuel online casino free vegas slots online casino | https://playonlinecasino.icu/ - las vegas casinos https://playonlinecasino.icu/ - jackpot party casino

real money casino mohegan sun casino san manuel online casino free vegas slots online casino | https://playonlinecasino.icu/ - las vegas casinos https://playonlinecasino.icu/ - jackpot party casino

muckleshoot casino virgin casino online gambling sites hard rock casino tampa | https://onlinecasinoo.icu/ - empire city casino online https://onlinecasinoo.icu/ - chumash casino

muckleshoot casino virgin casino online gambling sites hard rock casino tampa | https://onlinecasinoo.icu/ - empire city casino online https://onlinecasinoo.icu/ - chumash casino

borgata hotel casino online casinos harrah cherokee casino golden nugget online casino | https://playcasinoslots.ooo/ - downstream casino https://playcasinoslots.ooo/ - hyper casinos

Pages

Add new comment