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

o ed meds online without doctor prescription generic levitra vardenafil 20mg
ed meds online without doctor prescription [url=http://starlite.id/levitra/#]go to website[/url]
http://starlite.id/levitra/# levitra without a doctor prescription in usa

o ed meds online without doctor prescription generic levitra vardenafil 20mg
ed meds online without doctor prescription [url=http://starlite.id/levitra/#]go to website[/url]
http://starlite.id/levitra/# levitra without a doctor prescription in usa

http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...
http://smartowl.com.au/uncategorized/%d0%b8-%d0%bd%d0%b0%d0%bc-%d1%82%d0...

http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298798
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298841
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298857
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298875
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298883
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298888
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298892
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298902
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298928
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298947
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298957
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298983
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298995
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299002
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299043
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299049
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299070
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299073
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299083
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299102
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299111
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299119
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299133
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299138
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299176

http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298798
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298841
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298857
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298875
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298883
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298888
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298892
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298902
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298928
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298947
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298957
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298983
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=298995
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299002
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299043
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299049
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299070
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299073
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299083
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299102
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299111
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299119
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299133
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299138
http://khazovnh.bget.ru/?option=com_k2&view=itemlist&task=user&id=299176

http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62563
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62568
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62588
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62592
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62597
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62600
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62615
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62617
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62639
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62640
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62642
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62648
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62656
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62668
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62673
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62677
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62684
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62686
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62687
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62697
http://bustaniapartments.co.ke/index.php/component/k2/itemlist/user/62700

http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.giovaniconnection.it/?option=com_k2&view=itemlist&task=user&i...
http://www.giovaniconnection.it/?option=com_k2&view=itemlist&task=user&i...
http://www.giovaniconnection.it/?option=com_k2&view=itemlist&task=user&i...
http://www.giovaniconnection.it/?option=com_k2&view=itemlist&task=user&i...
http://www.giovaniconnection.it/?option=com_k2&view=itemlist&task=user&i...
http://www.giovaniconnection.it/?option=com_k2&view=itemlist&task=user&i...
http://www.giovaniconnection.it/?option=com_k2&view=itemlist&task=user&i...
http://www.giovaniconnection.it/?option=com_k2&view=itemlist&task=user&i...
http://www.giovaniconnection.it/?option=com_k2&view=itemlist&task=user&i...

http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.gioiellidisardegna.com/?option=com_k2&view=itemlist&task=user...
http://www.giovaniconnection.it/?option=com_k2&view=itemlist&task=user&i...
http://www.giovaniconnection.it/?option=com_k2&view=itemlist&task=user&i...
http://www.giovaniconnection.it/?option=com_k2&view=itemlist&task=user&i...
http://www.giovaniconnection.it/?option=com_k2&view=itemlist&task=user&i...
http://www.giovaniconnection.it/?option=com_k2&view=itemlist&task=user&i...
http://www.giovaniconnection.it/?option=com_k2&view=itemlist&task=user&i...
http://www.giovaniconnection.it/?option=com_k2&view=itemlist&task=user&i...
http://www.giovaniconnection.it/?option=com_k2&view=itemlist&task=user&i...
http://www.giovaniconnection.it/?option=com_k2&view=itemlist&task=user&i...

Pages

Add new comment