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

hearts of vegas free slots free vegas slots online casino wizard of oz slots free slots online | https://freecasinoslots.icu/ - caesars slots free casino https://freecasinoslots.icu/ - caesars free slots online

free slots casino games slot machine games free free slot games free slots machines | https://casinoslotsonline.icu/ - free vegas world slots https://casinoslotsonline.icu/ - slots free online

online casino bonus borgata online casino pala casino online nj muckleshoot casino | https://freeonlineslots.ooo/ - free vegas casino games https://freeonlineslots.ooo/ - real casino

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

caesars casino online winstar casino big fish casino slots ignition casino | https://vegascasino.icu/ - borgata online casino https://vegascasino.icu/ - jack online casino

caesars casino online winstar casino big fish casino slots ignition casino | https://vegascasino.icu/ - borgata online casino https://vegascasino.icu/ - jack online casino

turning stone online slots free vegas slots online cashman casino slots free slot play no download | https://casinoslotsonline.icu/ - free online slots no download no registration https://casinoslotsonline.icu/ - slots for free

free slots online no download free slots games online slot machines cleopatra slots | https://freecasinoslots.icu/ - free games for casino slots hollywood https://freecasinoslots.icu/ - liberty slots casino

three rivers casino foxwoods casino online free casino games slots posh casino | https://casinoplay.icu/ - casino near me https://casinoplay.icu/ - gambling sites

blue chip casino virgin online casino three rivers casino chumba casino | https://freeonlineslots.ooo/ - doubleu casino https://freeonlineslots.ooo/ - spirit mountain casino

oxford casino royal river casino sugarhouse casino online soaring eagle casino | https://casinovegas.ooo/ - big fish casino https://casinovegas.ooo/ - hard rock casino atlantic city

three rivers casino foxwoods casino online free casino games slots posh casino | https://casinoplay.icu/ - casino near me https://casinoplay.icu/ - gambling sites

oxford casino royal river casino sugarhouse casino online soaring eagle casino | https://casinovegas.ooo/ - big fish casino https://casinovegas.ooo/ - hard rock casino atlantic city

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

thunder valley casino mystic lake casino sugarhouse casino grand falls casino | https://casinovegas.ooo/ - winstar world casino https://casinovegas.ooo/ - pechanga casino

foxwoods casino free casino games no download gsn casino on facebook play casino | https://vegascasino.icu/ - huuuge casino https://vegascasino.icu/ - best online casinos

foxwoods casino free casino games no download gsn casino on facebook play casino | https://vegascasino.icu/ - huuuge casino https://vegascasino.icu/ - best online casinos

heart of vegas slots free slots no download hearts of vegas free slots casino games slots free | https://freecasinoslots.icu/ - pch slots https://freecasinoslots.icu/ - mirrorball slots

heart of vegas slots free slots no download hearts of vegas free slots casino games slots free | https://freecasinoslots.icu/ - pch slots https://freecasinoslots.icu/ - mirrorball slots

caesars casino casino slots online slot machines casino games | https://casinoplay.icu/ - winstar casino oklahoma https://casinoplay.icu/ - harrah cherokee casino

online slots wizard of oz slots da vinci diamonds free online slots simslots free slots | https://casinoslotsonline.icu/ - slots free games https://casinoslotsonline.icu/ - vegas slots

online slots wizard of oz slots da vinci diamonds free online slots simslots free slots | https://casinoslotsonline.icu/ - slots free games https://casinoslotsonline.icu/ - vegas slots

pala casino online dakota sioux casino casino games free free online casino games | https://freeonlineslots.ooo/ - stn play online casino https://freeonlineslots.ooo/ - casino game

play free vegas casino games free casino games vegas world online casinos online gambling casino | https://onlinecasinoplay.us.org/ - casino slots https://onlinecasinoplay.us.org/ - zone online casino

play free vegas casino games free casino games vegas world online casinos online gambling casino | https://onlinecasinoplay.us.org/ - casino slots https://onlinecasinoplay.us.org/ - zone online casino

oxford casino lincoln casino ignition casino blue chip casino | https://vegascasino.icu/ - free online casino slots https://vegascasino.icu/ - caesars casino online

oxford casino lincoln casino ignition casino blue chip casino | https://vegascasino.icu/ - free online casino slots https://vegascasino.icu/ - caesars casino online

spirit mountain casino fortune bay casino free casino games vegas world virgin casino online nj | https://casinovegas.ooo/ - online gambling sites https://casinovegas.ooo/ - hollywood casino play4fun

spirit mountain casino fortune bay casino free casino games vegas world virgin casino online nj | https://casinovegas.ooo/ - online gambling sites https://casinovegas.ooo/ - hollywood casino play4fun

free online slots vegas world vegas world slots free slots with no download or registration slots free online | https://freecasinoslots.icu/ - play free slots https://freecasinoslots.icu/ - free slots hollywood

free online slots vegas world vegas world slots free slots with no download or registration slots free online | https://freecasinoslots.icu/ - play free slots https://freecasinoslots.icu/ - free slots hollywood

hollywood casino online tropicana online casino four winds casino stn play online casino | https://freeonlineslots.ooo/ - hollywood casino https://freeonlineslots.ooo/ - casino online slots

free slots casino games dakota sioux casino plainridge casino playmgm nj casino online | https://casinoplay.icu/ - casino games https://casinoplay.icu/ - free vegas slots online casino

free slots casino games dakota sioux casino plainridge casino playmgm nj casino online | https://casinoplay.icu/ - casino games https://casinoplay.icu/ - free vegas slots online casino

borgata hotel casino free casino slot games oxford casino winstar world casino | https://casinovegas.ooo/ - free casino games slots https://casinovegas.ooo/ - hampton beach casino ballroom

big fish casino casino online slots play free vegas casino games free casino games online | https://onlinecasinoplay.us.org/ - play free vegas casino games https://onlinecasinoplay.us.org/ - online casinos

big fish casino casino online slots play free vegas casino games free casino games online | https://onlinecasinoplay.us.org/ - play free vegas casino games https://onlinecasinoplay.us.org/ - online casinos

gold fish casino slots pch slots tournament hollywood casino free slots free vegas world slots | https://freecasinoslots.icu/ - gold fish casino slots https://freecasinoslots.icu/ - vegas slots online free

mirrorball slots konami free slots liberty slots free slot play | https://casinoslotsonline.icu/ - vegas casino slots https://casinoslotsonline.icu/ - goldfish casino slots free

viagra alternative teva generic viagra does generic viagra work teva generic viagra

viagra alternative teva generic viagra does generic viagra work teva generic viagra

vegas casino games doubledown casino choctaw casino posh casino online | https://casinoplay.icu/ - slot machines https://casinoplay.icu/ - soaring eagle casino

vegas casino games doubledown casino choctaw casino posh casino online | https://casinoplay.icu/ - slot machines https://casinoplay.icu/ - soaring eagle casino

play free vegas casino games hard rock casino atlantic city pala casino online nj foxwoods casino | https://freeonlineslots.ooo/ - online casino real money https://freeonlineslots.ooo/ - bovada casino

rivers casino betfair casino online nj foxwoods casino del lago casino | https://vegascasino.icu/ - island view casino https://vegascasino.icu/ - vegas casino online

rivers casino betfair casino online nj foxwoods casino del lago casino | https://vegascasino.icu/ - island view casino https://vegascasino.icu/ - vegas casino online

buffalo gold slots caesars free slots online quick hit slots gsn casino slots | https://freecasinoslots.icu/ - free casino slot games https://freecasinoslots.icu/ - pop slots casino

muckleshoot casino gsn casino games free online casino games gsn casino games | https://vegascasino.icu/ - free casino games slotomania https://vegascasino.icu/ - zone online casino games

Pages

Add new comment