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

slotomania on facebook free casino slots games free vegas slots vegas casino slots | https://playslotsonline.icu/ - free vegas slots https://playslotsonline.icu/ - gsn casino slots

online gambling sites play online casino winstar casino red rock casino | https://onlinecasinorealmoney.ooo/ - pala casino online nj https://onlinecasinorealmoney.ooo/ - high five casino slots

online gambling sites play online casino winstar casino red rock casino | https://onlinecasinorealmoney.ooo/ - pala casino online nj https://onlinecasinorealmoney.ooo/ - high five casino slots

free casino games slotomania online casino gambling jack online casino free slots casino games | https://playslots.ooo/ - doubledown casino facebook https://playslots.ooo/ - doubleu casino on facebook

free casino games slotomania online casino gambling jack online casino free slots casino games | https://playslots.ooo/ - doubledown casino facebook https://playslots.ooo/ - doubleu casino on facebook

vegas free slots online freeslots.com slots mirrorball slots vegas free slots online | https://slotonline.icu/ - pop slots https://slotonline.icu/ - free slots slotomania

vegas free slots online freeslots.com slots mirrorball slots vegas free slots online | https://slotonline.icu/ - pop slots https://slotonline.icu/ - free slots slotomania

order viagra online generic viagra for sale viagra story when will generic viagra be available

online casino bonus harrah cherokee casino doubledown casino facebook online casinos for us players | https://playcasinogames.icu/ - hollywood casino online slots https://playcasinogames.icu/ - hollywood casino play4fun

online casino bonus harrah cherokee casino doubledown casino facebook online casinos for us players | https://playcasinogames.icu/ - hollywood casino online slots https://playcasinogames.icu/ - hollywood casino play4fun

online casino gambling doubledown casino online casinos | https://onlinecasino2018.us.org/ - casino games free online

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

seneca niagara casino soaring eagle casino mgm online casino nj turning stone casino | https://casino-bonus.icu/ - hollywood casino online slots https://casino-bonus.icu/ - rivers casino

seneca niagara casino soaring eagle casino mgm online casino nj turning stone casino | https://casino-bonus.icu/ - hollywood casino online slots https://casino-bonus.icu/ - rivers casino

free casino games slotomania casino games play online casino online casino games free | https://playslots.ooo/ - turning stone online casino https://playslots.ooo/ - plainridge casino

free casino games slotomania casino games play online casino online casino games free | https://playslots.ooo/ - turning stone online casino https://playslots.ooo/ - plainridge casino

virgin casino online nj casino game best online casinos caesars casino online | https://onlinecasinorealmoney.ooo/ - choctaw casino https://onlinecasinorealmoney.ooo/ - winstar casino oklahoma

caesars slots free casino hearts of vegas free slots goldfish casino slots free cashman casino slots | https://slotonline.icu/ - goldfish casino slots free https://slotonline.icu/ - slots for free

play online casino mgm online casino nj vegas casino games mohegan sun casino | https://playcasinogames.icu/ - paradise casino https://playcasinogames.icu/ - free casino

play online casino mgm online casino nj vegas casino games mohegan sun casino | https://playcasinogames.icu/ - paradise casino https://playcasinogames.icu/ - free casino

free slots 777 free video slots free buffalo slots free video slots | https://playslotsonline.icu/ - heart of vegas slots https://playslotsonline.icu/ - slot machine games free

bovada casino online gambling casino penny slots online casinos for us players | https://playslots.ooo/ - free casino games slots https://playslots.ooo/ - hypercasinos

bovada casino online gambling casino penny slots online casinos for us players | https://playslots.ooo/ - free casino games slots https://playslots.ooo/ - hypercasinos

harrah online casino casino near me winstar world casino posh casino | https://casino-bonus.icu/ - foxwoods casino online https://casino-bonus.icu/ - morongo casino

harrah online casino casino near me winstar world casino posh casino | https://casino-bonus.icu/ - foxwoods casino online https://casino-bonus.icu/ - morongo casino

greektown casino seneca niagara casino no deposit casino best online casinos | https://playcasinogames.icu/ - jackpot party casino https://playcasinogames.icu/ - gun lake casino

greektown casino seneca niagara casino no deposit casino best online casinos | https://playcasinogames.icu/ - jackpot party casino https://playcasinogames.icu/ - gun lake casino

high 5 casino free casino games sun moon free casino games online | https://onlinecasino2018.us.org/ - casino games free

high 5 casino free casino games sun moon free casino games online | https://onlinecasino2018.us.org/ - casino games free

play online casino four winds casino mgm online casino riverwind casino | https://onlinecasinorealmoney.ooo/ - sandia casino https://onlinecasinorealmoney.ooo/ - mohegan sun online casino

play online casino four winds casino mgm online casino riverwind casino | https://onlinecasinorealmoney.ooo/ - sandia casino https://onlinecasinorealmoney.ooo/ - mohegan sun online casino

hollywood casino online slots casino games online hollywood online casino casino games | https://casino-bonus.icu/ - free online casino games https://casino-bonus.icu/ - rock n cash casino slots

hollywood casino online slots casino games online hollywood online casino casino games | https://casino-bonus.icu/ - free online casino games https://casino-bonus.icu/ - rock n cash casino slots

mirrorball slots goldfish casino slots free slots for free free slots vegas | https://playslotsonline.icu/ - play slots https://playslotsonline.icu/ - casino slots free

mirrorball slots goldfish casino slots free slots for free free slots vegas | https://playslotsonline.icu/ - play slots https://playslotsonline.icu/ - casino slots free

liberty slots casino myvegas slots free casino slot games slots online | https://slotonline.icu/ - charlestown races and slots https://slotonline.icu/ - free casino slot games

liberty slots casino myvegas slots free casino slot games slots online | https://slotonline.icu/ - charlestown races and slots https://slotonline.icu/ - free casino slot games

riverwind casino hollywood casino play4fun casino bonus free casino | https://playslots.ooo/ - jackpot party casino facebook https://playslots.ooo/ - las vegas casinos

high 5 casino best online casinos online gambling mountaineer casino | https://playcasinogames.icu/ - play casino https://playcasinogames.icu/ - fortune bay casino

high 5 casino best online casinos online gambling mountaineer casino | https://playcasinogames.icu/ - play casino https://playcasinogames.icu/ - fortune bay casino

huuuge casino greektown casino three rivers casino chumash casino | https://onlinecasinorealmoney.ooo/ - gambling sites https://onlinecasinorealmoney.ooo/ - penny slots

huuuge casino greektown casino three rivers casino chumash casino | https://onlinecasinorealmoney.ooo/ - gambling sites https://onlinecasinorealmoney.ooo/ - penny slots

hollywood casino online gambling casino online gambling | https://onlinecasino2018.us.org/ - las vegas casinos

mohegan sun online casino zone online casino codeshareonline doubledown casino caesars online casino | https://casino-bonus.icu/ - casino game https://casino-bonus.icu/ - casino games slots free

slots of vegas casino free casino slots gold fish casino slots penny slots free online | https://playslotsonline.icu/ - play slots online https://playslotsonline.icu/ - free vegas slots online

goldfish casino slots free free slots slotomania free online casino slots play free slots for fun | https://slotonline.icu/ - house of fun free slots https://slotonline.icu/ - online slots

goldfish casino slots free free slots slotomania free online casino slots play free slots for fun | https://slotonline.icu/ - house of fun free slots https://slotonline.icu/ - online slots

vegas free slots online casino slots vegas slots online house of fun slots | https://playslotsonline.icu/ - hearts of vegas free slots https://playslotsonline.icu/ - goldfish casino slots free

Pages

Add new comment