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

casino games online turning stone online casino free casino games online doubleu casino on facebook | https://realmoneycasino.ooo/ - red rock casino https://realmoneycasino.ooo/ - foxwoods casino online slots

free games online slots slot machine games free buffalo slots goldfish slots | https://slotsrealmoney.icu/ - slots free https://slotsrealmoney.icu/ - online slot games

free games online slots slot machine games free buffalo slots goldfish slots | https://slotsrealmoney.icu/ - slots free https://slotsrealmoney.icu/ - online slot games

tropicana online casino doubledown casino las vegas casinos winstar world casino | https://onlinecasinoplay.us.org/ - online gambling https://onlinecasinoplay.us.org/ - casino games

gsn casino big fish casino gun lake casino sugarhouse casino online | https://onlinecasinoo.icu/ - free casino games sun moon https://onlinecasinoo.icu/ - free casino

gsn casino big fish casino gun lake casino sugarhouse casino online | https://onlinecasinoo.icu/ - free casino games sun moon https://onlinecasinoo.icu/ - free casino

casinos online vegas world casino games free slots games doubleu casino on facebook | https://playonlinecasino.icu/ - downstream casino https://playonlinecasino.icu/ - borgata online casino

generic viagra india viagra online generic buy cheap purchase uk viagra cheap generic viagra

generic viagra india viagra online generic buy cheap purchase uk viagra cheap generic viagra

casino slots free online casino games caesars online casino online casino real money | https://onlinecasinoplay.us.org/ - casino game https://onlinecasinoplay.us.org/ - casino games free online

casino slots free online casino games caesars online casino online casino real money | https://onlinecasinoplay.us.org/ - casino game https://onlinecasinoplay.us.org/ - casino games free online

downstream casino casino real money jack online casino jackpot party casino facebook | https://playcasinoslots.ooo/ - casino games online https://playcasinoslots.ooo/ - hollywood casino online

downstream casino casino real money jack online casino jackpot party casino facebook | https://playcasinoslots.ooo/ - casino games online https://playcasinoslots.ooo/ - hollywood casino online

best online slots cashman casino slots free slots hollywood las vegas free slots | https://slotgames.icu/ - slot machine games free https://slotgames.icu/ - free slot machine games

best online slots cashman casino slots free slots hollywood las vegas free slots | https://slotgames.icu/ - slot machine games free https://slotgames.icu/ - free slot machine games

morongo casino online casinos for us players winstar casino oklahoma potawatomi casino | https://onlinecasinoo.icu/ - rivers casino schenectady https://onlinecasinoo.icu/ - cherokee casino

free online casino slots free casino games online casino blackjack casino bonus codes | https://onlinecasinoplay.us.org/ - online gambling casino https://onlinecasinoplay.us.org/ - hypercasinos

free online casino slots free casino games online casino blackjack casino bonus codes | https://onlinecasinoplay.us.org/ - online gambling casino https://onlinecasinoplay.us.org/ - hypercasinos

play online casino hard rock casino tampa mystic lake casino foxwoods casino online slots | https://realmoneycasino.ooo/ - rock n cash casino slots https://realmoneycasino.ooo/ - online casino games

play online casino hard rock casino tampa mystic lake casino foxwoods casino online slots | https://realmoneycasino.ooo/ - rock n cash casino slots https://realmoneycasino.ooo/ - online casino games

online casino gambling free slots games online slot machines winstar world casino | https://playcasinoslots.ooo/ - casinos online https://playcasinoslots.ooo/ - penny slots

online casino gambling free slots games online slot machines winstar world casino | https://playcasinoslots.ooo/ - casinos online https://playcasinoslots.ooo/ - penny slots

goldfish slots slot games free free vegas slots online slots free | https://slotgames.icu/ - free video slots https://slotgames.icu/ - slots games

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

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

viagra online uk generic viagra online buy viagra soft viagra generic date

mohegan sun casino best online casinos caesars casino free vegas casino games | https://playonlinecasino.icu/ - san manuel casino https://playonlinecasino.icu/ - empire city casino online

free slots vegas world hot shot casino slots hollywood casino free slot play free online slots | https://slotsrealmoney.icu/ - free buffalo slots https://slotsrealmoney.icu/ - caesars free slots

free slots vegas world hot shot casino slots hollywood casino free slot play free online slots | https://slotsrealmoney.icu/ - free buffalo slots https://slotsrealmoney.icu/ - caesars free slots

buy viagra online without prescription does generic viagra work generic viagra online pharmacy generic viagra names

free casino games slots zone online casino games big fish casino free slots casino games | https://onlinecasinoplay.us.org/ - casino games slots free https://onlinecasinoplay.us.org/ - gsn casino games

free casino games slots zone online casino games big fish casino free slots casino games | https://onlinecasinoplay.us.org/ - casino games slots free https://onlinecasinoplay.us.org/ - gsn casino games

zone online casino free casino games online online casinos lady luck | https://onlinecasinoplay.us.org/ - las vegas casinos https://onlinecasinoplay.us.org/ - free casino games no download

zone online casino free casino games online online casinos lady luck | https://onlinecasinoplay.us.org/ - las vegas casinos https://onlinecasinoplay.us.org/ - free casino games no download

slots casino games winstar casino no deposit casino parx online casino | https://playonlinecasino.icu/ - slot machines https://playonlinecasino.icu/ - turning stone casino

slots casino games winstar casino no deposit casino parx online casino | https://playonlinecasino.icu/ - slot machines https://playonlinecasino.icu/ - turning stone casino

casino online slots bonus casino play online casino downstream casino | https://realmoneycasino.ooo/ - vegas casino online https://realmoneycasino.ooo/ - prairie meadows casino

online casino play casino best online casino play casino | https://onlinecasinoplay.us.org/ - big fish casino https://onlinecasinoplay.us.org/ - online casino games

online casino play casino best online casino play casino | https://onlinecasinoplay.us.org/ - big fish casino https://onlinecasinoplay.us.org/ - online casino games

slots casino games lincoln casino three rivers casino caesar casino online slot games | https://realmoneycasino.ooo/ - free casino games vegas world https://realmoneycasino.ooo/ - online casino games free

slots casino games lincoln casino three rivers casino caesar casino online slot games | https://realmoneycasino.ooo/ - free casino games vegas world https://realmoneycasino.ooo/ - online casino games free

free casino games online best online casinos free online slots online casinos | https://onlinecasinoplay.us.org/ - firekeepers casino https://onlinecasinoplay.us.org/ - best online casino

free casino games online best online casinos free online slots online casinos | https://onlinecasinoplay.us.org/ - firekeepers casino https://onlinecasinoplay.us.org/ - best online casino

online casino caesars casino dakota sioux casino empire city casino online | https://realmoneycasino.ooo/ - royal river casino https://realmoneycasino.ooo/ - betfair casino online nj

liberty slots casino quick hit slots free slots vegas play slots online | https://slotgames.icu/ - ruby slots https://slotgames.icu/ - online slots free

liberty slots casino quick hit slots free slots vegas play slots online | https://slotgames.icu/ - ruby slots https://slotgames.icu/ - online slots free

doubleu casino betfair casino online nj free vegas casino games golden nugget online casino | https://playcasinoslots.ooo/ - online casinos for us players https://playcasinoslots.ooo/ - vegas world casino games

doubleu casino betfair casino online nj free vegas casino games golden nugget online casino | https://playcasinoslots.ooo/ - online casinos for us players https://playcasinoslots.ooo/ - vegas world casino games

Pages

Add new comment