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

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

online casino gambling las vegas casinos casino bonus | https://onlinecasino888.us.org/ - casino bonus codes

online casino gambling las vegas casinos casino bonus | https://onlinecasino888.us.org/ - casino bonus codes

Издавна веков рыбалка являлась самым из стержневых пристрастий представителей сильного пола. Если уж вначале рыболовство прокармливало своей семьи и подсобляло с этой помощью оставаться в живых, то в данный момент оно переросло в страсть теперешних мужчин и иногда представительниц прекрасного пола. Рыбалка в Российской федерации довольно востребована. Выискать ясное разъяснение сему совершенно не так-то легко. Одной из ключевых оснований считается традиция. С незапамятных времен добыча питания являлась основной функцией представителей сильного пола.
Подробнее:
прикормка горох на леща

cashman casino slots play free slots for fun free slots hollywood infinity slots | https://slotonline.icu/ - slots for free https://slotonline.icu/ - liberty slots

cashman casino slots play free slots for fun free slots hollywood infinity slots | https://slotonline.icu/ - slots for free https://slotonline.icu/ - liberty slots

pala casino maryland live casino online vegas casino online potawatomi casino | https://casino-bonus.icu/ - jackpot party casino https://casino-bonus.icu/ - ilani casino

pala casino maryland live casino online vegas casino online potawatomi casino | https://casino-bonus.icu/ - jackpot party casino https://casino-bonus.icu/ - ilani casino

casino game gun lake casino thunder valley casino viejas casino | https://playcasinogames.icu/ - harrah cherokee casino https://playcasinogames.icu/ - online gambling casino

casino game gun lake casino thunder valley casino viejas casino | https://playcasinogames.icu/ - harrah cherokee casino https://playcasinogames.icu/ - online gambling casino

hypercasinos empire city casino rivers casino schenectady free casino games | https://onlinecasinorealmoney.ooo/ - hard rock casino tampa https://onlinecasinorealmoney.ooo/ - free casino games slots

hypercasinos empire city casino rivers casino schenectady free casino games | https://onlinecasinorealmoney.ooo/ - hard rock casino tampa https://onlinecasinorealmoney.ooo/ - free casino games slots

jackpot magic slots free online slots vegas world slot machine games free free slots no download no registration | https://playslotsonline.icu/ - free slots slotomania https://playslotsonline.icu/ - vegas slots online

jackpot magic slots free online slots vegas world slot machine games free free slots no download no registration | https://playslotsonline.icu/ - free slots slotomania https://playslotsonline.icu/ - vegas slots online

choctaw casino online casino games zone online casino games online casino bonus | https://playslots.ooo/ - paradise casino https://playslots.ooo/ - three rivers casino

free online slots no download casino games slots free infinity slots slots for free | https://slotonline.icu/ - free slot games https://slotonline.icu/ - liberty slots casino

free online casino free online casino slots free casino games online | https://onlinecasino888.us.org/ - play casino

casinos in iowa snoqualmie casino play casino slots for real money | https://casino-bonus.icu/ - zone online casino games https://casino-bonus.icu/ - oxford casino

casinos in iowa snoqualmie casino play casino slots for real money | https://casino-bonus.icu/ - zone online casino games https://casino-bonus.icu/ - oxford casino

viejas casino casino real money winstar world casino del lago casino | https://playcasinogames.icu/ - double u casino https://playcasinogames.icu/ - winstar casino oklahoma

celebrity slots free slots jackpot magic slots my vegas slots | https://playslotsonline.icu/ - free casino slots no download https://playslotsonline.icu/ - slots online free

celebrity slots free slots jackpot magic slots my vegas slots | https://playslotsonline.icu/ - free casino slots no download https://playslotsonline.icu/ - slots online free

doubleu casino on facebook chinook winds casino doubledown casino choctaw casino | https://onlinecasinorealmoney.ooo/ - hollywood casino https://onlinecasinorealmoney.ooo/ - cafe casino online

doubleu casino on facebook chinook winds casino doubledown casino choctaw casino | https://onlinecasinorealmoney.ooo/ - hollywood casino https://onlinecasinorealmoney.ooo/ - cafe casino online

empire casino online greektown casino mgm online casino casinos online | https://playslots.ooo/ - penny slots https://playslots.ooo/ - mgm online casino nj

slots free games pch slots liberty slots casino slot games free | https://slotonline.icu/ - free slots casino games https://slotonline.icu/ - free online slots no download no registration

winstar world casino rock n cash casino slots las vegas casinos treasure island casino minnesota | https://casino-bonus.icu/ - greektown casino https://casino-bonus.icu/ - three rivers casino

winstar world casino rock n cash casino slots las vegas casinos treasure island casino minnesota | https://casino-bonus.icu/ - greektown casino https://casino-bonus.icu/ - three rivers casino

soaring eagle casino free casino games online parx casino jackpot party casino facebook | https://onlinecasinorealmoney.ooo/ - free casino games slot machines https://onlinecasinorealmoney.ooo/ - doubleu casino on facebook

soaring eagle casino free casino games online parx casino jackpot party casino facebook | https://onlinecasinorealmoney.ooo/ - free casino games slot machines https://onlinecasinorealmoney.ooo/ - doubleu casino on facebook

slot machines ruby slots vegas free slots online doubledown casino free slots | https://playslotsonline.icu/ - hollywood slots https://playslotsonline.icu/ - slots free

stn play online casino river spirit casino tulsa slotomania slot machines cafe casino online | https://playcasinogames.icu/ - plainridge casino https://playcasinogames.icu/ - vegas world casino games

stn play online casino river spirit casino tulsa slotomania slot machines cafe casino online | https://playcasinogames.icu/ - plainridge casino https://playcasinogames.icu/ - vegas world casino games

chumba casino free casino tropicana online casino | https://onlinecasino888.us.org/ - online casino real money

chumba casino free casino tropicana online casino | https://onlinecasino888.us.org/ - online casino real money

free slots vegas world caesars slots liberty slots caesars slots free casino | https://slotonline.icu/ - cleopatra slots https://slotonline.icu/ - pop slots

no deposit casino cafe casino online gambling sites casino slot | https://playslots.ooo/ - firekeepers casino https://playslots.ooo/ - lady luck

no deposit casino cafe casino online gambling sites casino slot | https://playslots.ooo/ - firekeepers casino https://playslots.ooo/ - lady luck

foxwoods casino online northern quest casino online casino reviews hampton beach casino ballroom | https://casino-bonus.icu/ - casino games https://casino-bonus.icu/ - hollywood casino

foxwoods casino online northern quest casino online casino reviews hampton beach casino ballroom | https://casino-bonus.icu/ - casino games https://casino-bonus.icu/ - hollywood casino

betfair casino online nj pala casino online harrah cherokee casino rivers casino pittsburgh | https://onlinecasinorealmoney.ooo/ - blue chip casino https://onlinecasinorealmoney.ooo/ - paradise casino

potawatomi casino viejas casino real casino slots royal river casino | https://playcasinogames.icu/ - caesars casino https://playcasinogames.icu/ - empire city casino

potawatomi casino viejas casino real casino slots royal river casino | https://playcasinogames.icu/ - caesars casino https://playcasinogames.icu/ - empire city casino

casino bonus codes potawatomi casino zone online casino online casino gambling | https://playslots.ooo/ - paradise casino https://playslots.ooo/ - free vegas casino games

casino bonus codes potawatomi casino zone online casino online casino gambling | https://playslots.ooo/ - paradise casino https://playslots.ooo/ - free vegas casino games

free casino games vegas world casino play free casino games vegas world | https://onlinecasino888.us.org/ - gsn casino games

goldfish slots free slot games with no download freeslots.com slots online slots | https://slotonline.icu/ - slotomania free slots https://slotonline.icu/ - konami free slots

goldfish slots free slot games with no download freeslots.com slots online slots | https://slotonline.icu/ - slotomania free slots https://slotonline.icu/ - konami free slots

winstar casino online gambling rock n cash casino slots harrah cherokee casino | https://onlinecasinorealmoney.ooo/ - caesars casino https://onlinecasinorealmoney.ooo/ - casino slot

casinos near me hollywood casino play4fun chumash casino high 5 casino | https://casino-bonus.icu/ - casino games free online https://casino-bonus.icu/ - casino bonus

Pages

Add new comment