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

stn play online casino jackpot party casino free casino games slots harrah cherokee casino | https://playonlinecasino.icu/ - casino games free https://playonlinecasino.icu/ - casino slot

stn play online casino jackpot party casino free casino games slots harrah cherokee casino | https://playonlinecasino.icu/ - casino games free https://playonlinecasino.icu/ - casino slot

pompeii slots hot shot casino slots free penny slots online real casino slots | https://slotgames.icu/ - online slots free https://slotgames.icu/ - buffalo slots

pompeii slots hot shot casino slots free penny slots online real casino slots | https://slotgames.icu/ - online slots free https://slotgames.icu/ - buffalo slots

online casinos sandia casino online casino games free parx casino online | https://realmoneycasino.ooo/ - mountaineer casino https://realmoneycasino.ooo/ - rivers casino

online casinos sandia casino online casino games free parx casino online | https://realmoneycasino.ooo/ - mountaineer casino https://realmoneycasino.ooo/ - rivers casino

ignition casino hollywood casino lincoln casino pala casino online | https://playcasinoslots.ooo/ - borgata online casino https://playcasinoslots.ooo/ - lincoln casino

ignition casino hollywood casino lincoln casino pala casino online | https://playcasinoslots.ooo/ - borgata online casino https://playcasinoslots.ooo/ - lincoln casino

vegas world free slots games free slots games online vegas free slots old vegas slots | https://slotsrealmoney.icu/ - jackpot magic slots https://slotsrealmoney.icu/ - free online slots

vegas world free slots games free slots games online vegas free slots old vegas slots | https://slotsrealmoney.icu/ - jackpot magic slots https://slotsrealmoney.icu/ - free online slots

bovada casino 888 casino winstar casino oklahoma online casinos for us players | https://onlinecasinoo.icu/ - foxwoods casino online slots https://onlinecasinoo.icu/ - cherokee casino

bovada casino 888 casino winstar casino oklahoma online casinos for us players | https://onlinecasinoo.icu/ - foxwoods casino online slots https://onlinecasinoo.icu/ - cherokee casino

slots for real money casino near me vegas casino online vegas casino games | https://realmoneycasino.ooo/ - jackpot party casino https://realmoneycasino.ooo/ - casino games

free vegas slots online casino free online slots turning stone online slots caesars slots free casino | https://slotgames.icu/ - liberty slots casino https://slotgames.icu/ - free slot play no download

doubleu casino on facebook soaring eagle casino greektown casino chumba casino | https://playcasinoslots.ooo/ - downstream casino https://playcasinoslots.ooo/ - empire city online casino

foxwoods casino online best online casinos four winds casino spirit mountain casino | https://playonlinecasino.icu/ - posh casino https://playonlinecasino.icu/ - red rock casino

slot games free online slots vegas world wizard of oz slots old vegas slots | https://slotsrealmoney.icu/ - free online slots no download no registration https://slotsrealmoney.icu/ - vegas slots

slot games free online slots vegas world wizard of oz slots old vegas slots | https://slotsrealmoney.icu/ - free online slots no download no registration https://slotsrealmoney.icu/ - vegas slots

sugarhouse online casino free casino games online caesar casino online slot games mgm online casino | https://onlinecasinoo.icu/ - empire casino online https://onlinecasinoo.icu/ - island view casino

high five casino slots hollywood casino river spirit casino tulsa free slots casino games | https://playcasinoslots.ooo/ - cashman casino https://playcasinoslots.ooo/ - zone online casino games

gsn casino games mystic lake casino plainridge casino free casino games vegas world | [url=https://realmoneycasino.ooo/]pala casino online[/url] [url=https://realmoneycasino.ooo/]pechanga casino[/url]

gsn casino games mystic lake casino plainridge casino free casino games vegas world | [url=https://realmoneycasino.ooo/]pala casino online[/url] [url=https://realmoneycasino.ooo/]pechanga casino[/url]

free casino slots no download free vegas slots online penny slots free free vegas slots | https://slotgames.icu/ - goldfish casino slots free https://slotgames.icu/ - house of fun slots

free casino slots no download free vegas slots online penny slots free free vegas slots | https://slotgames.icu/ - goldfish casino slots free https://slotgames.icu/ - house of fun slots

world class casino slots hollywood casino free slots online pop slots casino free online slots games | https://slotsrealmoney.icu/ - play slots online https://slotsrealmoney.icu/ - freeslots.com slots

world class casino slots hollywood casino free slots online pop slots casino free online slots games | https://slotsrealmoney.icu/ - play slots online https://slotsrealmoney.icu/ - freeslots.com slots

casino games online casino real money free casino games sun moon big fish casino | https://onlinecasinoplay.us.org/ - hollywood casino https://onlinecasinoplay.us.org/ - gsn casino

casino games online casino real money free casino games sun moon big fish casino | https://onlinecasinoplay.us.org/ - hollywood casino https://onlinecasinoplay.us.org/ - gsn casino

river spirit casino tulsa seneca niagara casino vegas casino games northern quest casino | https://playonlinecasino.icu/ - casino games slots free https://playonlinecasino.icu/ - casinos near me

river spirit casino tulsa seneca niagara casino vegas casino games northern quest casino | https://playonlinecasino.icu/ - casino games slots free https://playonlinecasino.icu/ - casinos near me

free casino games slots slot machines jackpot party casino treasure island casino minnesota | https://onlinecasinoo.icu/ - free vegas casino games https://onlinecasinoo.icu/ - lady luck online casino

free casino games slots slot machines jackpot party casino treasure island casino minnesota | https://onlinecasinoo.icu/ - free vegas casino games https://onlinecasinoo.icu/ - lady luck online casino

hollywood casino winstar world casino hyper casinos choctaw casino | https://realmoneycasino.ooo/ - jackpot party casino https://realmoneycasino.ooo/ - no deposit casino

free casino slots no download free slots with no download or registration online casino slots celebrity slots | https://slotsrealmoney.icu/ - slots for free https://slotsrealmoney.icu/ - free vegas slots online casino

morongo casino real casino slots real casino slots double u casino | https://playcasinoslots.ooo/ - choctaw casino durant oklahoma https://playcasinoslots.ooo/ - doubledown casino promo codes

simslots free slots slots free online gossip slots casino slots free games | https://slotgames.icu/ - vegas world free slots games https://slotgames.icu/ - konami free slots

slots for real money prairie meadows casino blue chip casino gsn casino games | https://onlinecasinoo.icu/ - meadows casino https://onlinecasinoo.icu/ - penny slots

posh casino online sandia casino best online casino pechanga casino | https://playonlinecasino.icu/ - slot machines https://playonlinecasino.icu/ - casino real money

posh casino online sandia casino best online casino pechanga casino | https://playonlinecasino.icu/ - slot machines https://playonlinecasino.icu/ - casino real money

virgin online casino caesars online casino casino play free online casino slots | https://onlinecasinoplay.us.org/ - free casino games sun moon https://onlinecasinoplay.us.org/ - slots for real money

penny slots free vegas world free slots games free online slots buffalo slots | https://slotsrealmoney.icu/ - liberty slots casino https://slotsrealmoney.icu/ - slot machine games free

doubledown casino promo codes mountaineer casino online casinos for us players paradise casino | https://realmoneycasino.ooo/ - play casino https://realmoneycasino.ooo/ - best online casinos

seneca niagara casino bovada blackjack greektown casino playmgm nj casino online | https://playcasinoslots.ooo/ - winstar casino oklahoma https://playcasinoslots.ooo/ - red rock casino

seneca niagara casino bovada blackjack greektown casino playmgm nj casino online | https://playcasinoslots.ooo/ - winstar casino oklahoma https://playcasinoslots.ooo/ - red rock casino

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

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

Pages

Add new comment