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

online casino las vegas casinos free online casino games | https://onlinecasino2018.us.org/ - free slots casino games

borgata online casino firekeepers casino casino games | https://onlinecasino2018.us.org/ - hollywood casino

borgata online casino firekeepers casino casino games | https://onlinecasino2018.us.org/ - hollywood casino

casino games free foxwoods online casino casino real money | https://onlinecasino2018.us.org/ - online casino

casino games free foxwoods online casino casino real money | https://onlinecasino2018.us.org/ - online casino

slots for real money online casino borgata online casino | https://onlinecasino2018.us.org/ - free vegas casino games

vegas world casino games free casino games free online casino games | https://onlinecasino2018.us.org/ - casino slots

vegas world casino games free casino games free online casino games | https://onlinecasino2018.us.org/ - casino slots

free casino games vegas world casino play free casino games no download | https://onlinecasino2018.us.org/ - caesars online casino

free casino games vegas world casino play free casino games no download | https://onlinecasino2018.us.org/ - caesars online casino

best online casinos parx online casino virgin online casino | https://onlinecasino2018.us.org/ - online casino

online casino gambling casino slots big fish casino | https://onlinecasino2018.us.org/ - casino blackjack

online casino gambling casino slots big fish casino | https://onlinecasino2018.us.org/ - casino blackjack

free casino games slot machines casino bonus codes online casino gambling | https://onlinecasino2018.us.org/ - free online casino slots

online casinos for us players casino games slots free online casinos | https://onlinecasino2018.us.org/ - play free vegas casino games

casino games free online lady luck online casino no deposit casino | https://onlinecasino2018.us.org/ - virgin online casino

casino games free online lady luck online casino no deposit casino | https://onlinecasino2018.us.org/ - virgin online casino

free casino games sun moon real casino zone online casino games | https://onlinecasino2018.us.org/ - casino play

online casino gambling casino games online gsn casino games free casino games online | https://onlinecasinoplay.us.org/ - casino games free online https://onlinecasinoplay.us.org/ - free casino games vegas world

Детский вебсайт, организован намеренно для Вас самих и всех ваших деток. Главной целью мероприятия несомненно является - отрезок времени препровождения с толком, радостью и радостно. Надеемся смотреть детский сервис станет интересно отнюдь не обязательно юным матерям, но и опытнейшим родителям с опытом. Хотелось бы, затем чтобы указанный детский сервис стал вам лично надежным приятелем и советчиком.
Подробнее:
дети маленькие

zone online casino games online casinos online casino bonus online casino bonus | https://onlinecasinoplay.us.org/ - virgin online casino https://onlinecasinoplay.us.org/ - play online casino

free casino games online free online casino slots doubledown casino free casino games online | [url=https://onlinecasinodd.com/]zone online casino[/url] [url=https://onlinecasinodd.com/]casino play[/url]

hyper casinos borgata online casino online casino bonus virgin online casino | https://onlinecasinoplay.us.org/ - free casino games online https://onlinecasinoplay.us.org/ - casino games free

firekeepers casino gsn casino slots casino games slots free foxwoods online casino | https://onlinecasinoplay.us.org/ - online casino https://onlinecasinoplay.us.org/ - high 5 casino

free casino games slot machines tropicana online casino free casino games caesars online casino | [url=https://onlinecasinoplay.us.org/]free casino games[/url] [url=https://onlinecasinoplay.us.org/]casino bonus codes[/url]

play online casino casino games online play online casino foxwoods online casino | https://onlinecasinoplay.us.org/ - online casino games https://onlinecasinoplay.us.org/ - casino real money

play online casino casino games online play online casino foxwoods online casino | https://onlinecasinoplay.us.org/ - online casino games https://onlinecasinoplay.us.org/ - casino real money

free online slots casino real money empire city online casino online casinos | https://onlinecasinoplay.us.org/ - free casino games online https://onlinecasinoplay.us.org/ - mgm online casino

free online slots casino real money empire city online casino online casinos | https://onlinecasinoplay.us.org/ - free casino games online https://onlinecasinoplay.us.org/ - mgm online casino

casino online slots casino bonus codes real casino casino bonus codes | https://onlinecasinoplay.us.org/ - casino online https://onlinecasinoplay.us.org/ - no deposit casino

online casinos for us players casino blackjack gsn casino slots hypercasinos | https://onlinecasinoplay.us.org/ - casino slots https://onlinecasinoplay.us.org/ - tropicana online casino

online casinos for us players casino blackjack gsn casino slots hypercasinos | https://onlinecasinoplay.us.org/ - casino slots https://onlinecasinoplay.us.org/ - tropicana online casino

casino slots casino games slots for real money casino real money | https://onlinecasinoplay.us.org/ - free online casino slots https://onlinecasinoplay.us.org/ - free casino games no download

casino slots casino games slots for real money casino real money | https://onlinecasinoplay.us.org/ - free online casino slots https://onlinecasinoplay.us.org/ - free casino games no download

empire city online casino free online casino online gambling free casino games slotomania | https://onlinecasinoplay.us.org/ - slots for real money https://onlinecasinoplay.us.org/ - real casino

Pages

Add new comment