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

cheap generic viagra cheap generic viagra free viagra samples generic viagra canada

cheap generic viagra cheap generic viagra free viagra samples generic viagra canada

del lago casino real money casino free online casino empire city casino online | https://casino-bonus.icu/ - high five casino slots https://casino-bonus.icu/ - big fish casino

online casinos playmgm nj casino online casino near me foxwoods casino online | https://playcasinogames.icu/ - bigfish casino online games https://playcasinogames.icu/ - no deposit casino

online casinos playmgm nj casino online casino near me foxwoods casino online | https://playcasinogames.icu/ - bigfish casino online games https://playcasinogames.icu/ - no deposit casino

hollywood casino free slots online free slot machine games online slot games free slots slotomania | https://slotonline.icu/ - vegas world slots https://slotonline.icu/ - play slots online

cashman casino slots free free slots play free slots free slots machines | https://playslotsonline.icu/ - free games online slots https://playslotsonline.icu/ - quick hit slots

cherokee casino parx casino free online slots paradise casino | https://playslots.ooo/ - mohegan sun casino https://playslots.ooo/ - online slot machines

hollywood casino play4fun pala casino gambling sites huuuge casino | https://onlinecasinorealmoney.ooo/ - 888 casino https://onlinecasinorealmoney.ooo/ - riverwind casino

hollywood casino play4fun pala casino gambling sites huuuge casino | https://onlinecasinorealmoney.ooo/ - 888 casino https://onlinecasinorealmoney.ooo/ - riverwind casino

firekeepers casino mohegan sun online casino foxwoods casino free casino | https://playslots.ooo/ - sugarhouse online casino https://playslots.ooo/ - casino games slots free

casino bonus codes casino real money high 5 casino | https://onlinecasino888.us.org/ - free casino games sun moon

island view casino huuuge casino slotomania slot machines rivers casino pittsburgh | https://playcasinogames.icu/ - zone online casino games https://playcasinogames.icu/ - winstar casino oklahoma

parx online casino hollywood online casino casino bonus doubleu casino on facebook | https://casino-bonus.icu/ - zone online casino games https://casino-bonus.icu/ - bonus casino

parx online casino hollywood online casino casino bonus doubleu casino on facebook | https://casino-bonus.icu/ - zone online casino games https://casino-bonus.icu/ - bonus casino

seneca allegany casino greektown casino empire casino online hyper casinos | https://onlinecasinorealmoney.ooo/ - best online casinos https://onlinecasinorealmoney.ooo/ - penny slots

online slots play free slots for fun vegas world free slots games slot games | https://playslotsonline.icu/ - quick hit slots https://playslotsonline.icu/ - penny slots free online

buffalo slots casino slots slot machines scatter slots | https://slotonline.icu/ - pop slots https://slotonline.icu/ - myvegas slots

zone online casino ignition casino stn play online casino free casino games vegas world | https://playslots.ooo/ - free online casino https://playslots.ooo/ - empire city casino

slots online free free vegas slots free slot play no download pop slots | https://playslotsonline.icu/ - free vegas slots online casino https://playslotsonline.icu/ - free slots no download no registration

casino bonus codes doubledown casino facebook turning stone casino casino games free | https://onlinecasinorealmoney.ooo/ - online casino reviews https://onlinecasinorealmoney.ooo/ - hyper casinos

casino games free mystic lake casino mohegan sun casino bigfish casino online games | [url=https://playslots.ooo/]fortune bay casino[/url] [url=https://playslots.ooo/]virgin casino online[/url]

posh casino online del lago casino playmgm nj casino online empire casino online | https://playcasinogames.icu/ - casino games free https://playcasinogames.icu/ - prairie meadows casino

free video slots scatter slots hollywood casino free slots online free slots slotomania | https://slotonline.icu/ - free slots games online https://slotonline.icu/ - scatter slots

mountaineer casino casino games slots free online casino real money morongo casino | https://casino-bonus.icu/ - hollywood online casino https://casino-bonus.icu/ - virgin online casino

mountaineer casino casino games slots free online casino real money morongo casino | https://casino-bonus.icu/ - hollywood online casino https://casino-bonus.icu/ - virgin online casino

vegas world casino games casino online slots tropicana online casino | https://onlinecasino888.us.org/ - online casino slots

empire city online casino big fish casino mgm online casino nj chinook winds casino | https://onlinecasinorealmoney.ooo/ - turning stone online casino https://onlinecasinorealmoney.ooo/ - cafe casino online

play free slots pch slots free penny slots online free penny slots online | https://playslotsonline.icu/ - freeslots.com slots https://playslotsonline.icu/ - myvegas slots

casino games slots free las vegas casinos slots casino games playmgm nj casino online | https://playslots.ooo/ - real casino slots https://playslots.ooo/ - free slots casino games

pompeii slots slot games free buffalo slots pop slots casino | https://slotonline.icu/ - las vegas free slots https://slotonline.icu/ - my vegas slots

hollywood online casino chumba casino pechanga casino blue chip casino | https://playcasinogames.icu/ - rivers casino schenectady https://playcasinogames.icu/ - jack online casino

hollywood online casino chumba casino pechanga casino blue chip casino | https://playcasinogames.icu/ - rivers casino schenectady https://playcasinogames.icu/ - jack online casino

prairie meadows casino online casinos for us players empire city online casino free casino games slotomania | https://casino-bonus.icu/ - free casino games no download https://casino-bonus.icu/ - no deposit casino

prairie meadows casino online casinos for us players empire city online casino free casino games slotomania | https://casino-bonus.icu/ - free casino games no download https://casino-bonus.icu/ - no deposit casino

casino games free online casino game bovada casino | https://onlinecasino888.us.org/ - free casino games

casino games free online casino game bovada casino | https://onlinecasino888.us.org/ - free casino games

free online casino games casino games online gambling casino winstar world casino | https://onlinecasinorealmoney.ooo/ - casino real money https://onlinecasinorealmoney.ooo/ - paradise casino

free slot play no download vegas slots online free cashman casino slots free vegas slots | https://playslotsonline.icu/ - slots games https://playslotsonline.icu/ - pop slots casino

online casino bonus casino games free online doubleu casino on facebook doubledown casino | https://playslots.ooo/ - twin river casino https://playslots.ooo/ - prairie meadows casino

best online slots vegas world slots goldfish casino slots free slots games free | https://slotonline.icu/ - quick hit slots https://slotonline.icu/ - free casino slots no download

high 5 casino mgm online casino nj real money casino betfair casino online nj | https://casino-bonus.icu/ - paradise casino https://casino-bonus.icu/ - casino real money

high 5 casino mgm online casino nj real money casino betfair casino online nj | https://casino-bonus.icu/ - paradise casino https://casino-bonus.icu/ - casino real money

free online casino games free online slots hard rock casino tampa downstream casino | https://playcasinogames.icu/ - chumash casino https://playcasinogames.icu/ - codeshareonline doubledown casino

free online casino games free online slots hard rock casino tampa downstream casino | https://playcasinogames.icu/ - chumash casino https://playcasinogames.icu/ - codeshareonline doubledown casino

free vegas casino games casino online slots morongo casino online casino real money | [url=https://onlinecasinorealmoney.ooo/]virgin online casino[/url] [url=https://onlinecasinorealmoney.ooo/]online casino reviews[/url]

free vegas casino games casino online slots morongo casino online casino real money | [url=https://onlinecasinorealmoney.ooo/]virgin online casino[/url] [url=https://onlinecasinorealmoney.ooo/]online casino reviews[/url]

free slot games slots free online free slots casino games free slots vegas | https://playslotsonline.icu/ - best online slots https://playslotsonline.icu/ - caesars slots free casino

free slot games slots free online free slots casino games free slots vegas | https://playslotsonline.icu/ - best online slots https://playslotsonline.icu/ - caesars slots free casino

usa online casino virgin casino online caesar casino online slot games las vegas casinos | https://playslots.ooo/ - parx casino https://playslots.ooo/ - hollywood casino online slots

Pages

Add new comment