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

z view site buy cialis in mexico
purchasing cialis in mexico [url=https://buycialisinmexico.com#]buy cialis in mexico[/url]
https://buycialisinmexico.com# cialis coupon

z view site buy cialis in mexico
purchasing cialis in mexico [url=https://buycialisinmexico.com#]buy cialis in mexico[/url]
https://buycialisinmexico.com# cialis coupon

a http://viagrawithoutadoctorprescriptionneu.com# viagra without a doctor prescription usa
how long does viagra stay in your system
viagra without a doctors prescription
viagra without a doctor prescription usa
viagra without doctor prescription [url=http://viagrawithoutadoctorprescriptionneu.com#]viagra without prescription[/url]

Perfect update of captchas recognition software "XRumer 16.0 + XEvil":
captcha solving of Google (ReCaptcha-2 and ReCaptcha-3), Facebook, BitFinex, Bing, Hotmail, SolveMedia, Yandex,
and more than 8400 another size-types of captchas,
with highest precision (80..100%) and highest speed (100 img per second).
You can use XEvil 4.0 with any most popular SEO/SMM programms: iMacros, XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software.

Interested? There are a lot of impessive videos about XEvil in YouTube.

FREE DEMO AVAILABLE!

See you later!

http://XEvil.net/

y http://sildenafilneu.com# sildenafil coupons
sildenafil citrate 100mg tab
check this out
sildenafil 100mg cost
sildenafil dosage for men [url=http://sildenafilneu.com#]sildenafil citrate generic viagra 100mg[/url]

y http://sildenafilneu.com# sildenafil coupons
sildenafil citrate 100mg tab
check this out
sildenafil 100mg cost
sildenafil dosage for men [url=http://sildenafilneu.com#]sildenafil citrate generic viagra 100mg[/url]

w cialis from mexico cialis coupon
cialis [url=http://cialisneu.com#]cialis online pharmacy[/url]
http://cialisneu.com# purchasing cialis in mexico

w cialis from mexico cialis coupon
cialis [url=http://cialisneu.com#]cialis online pharmacy[/url]
http://cialisneu.com# purchasing cialis in mexico

f cialis coupon cialis online
cialis online [url=http://cialisneu.com#]buy cialis online[/url]
http://cialisneu.com# cialis from mexico

f cialis coupon cialis online
cialis online [url=http://cialisneu.com#]buy cialis online[/url]
http://cialisneu.com# cialis from mexico

w http://sildenafilneu.com# sildenafil citrate 100mg lowest price
sildenafil 20 mg tablet
sildenafil citrate generic viagra 100mg
view site
sildenafil 20 mg tablet [url=http://sildenafilneu.com#]sildenafil 100mg cost[/url]

w http://sildenafilneu.com# sildenafil citrate 100mg lowest price
sildenafil 20 mg tablet
sildenafil citrate generic viagra 100mg
view site
sildenafil 20 mg tablet [url=http://sildenafilneu.com#]sildenafil 100mg cost[/url]

n generic viagra without prescription generic viagra without prescription
viagra no prior prescription [url=http://viagrawithoutadoctorprescriptionneu.com#]tm eye ltd[/url]
http://viagrawithoutadoctorprescriptionneu.com# viagra without a doctors prescription

j http://viagrawithoutadoctorprescriptionneu.com# canada viagra
viagra over counter
viagra without prescription
viagra without a doctor prescription
viagra without prescription [url=http://viagrawithoutadoctorprescriptionneu.com#]viagra without a doctors prescription[/url]

b sildenafil citrate 100mg lowest price sildenafil citrate 100mg lowest price
sildenafil citrate 100mg tab [url=http://sildenafilneu.com#]sildenafil 100mg price[/url]
http://sildenafilneu.com# when will generic viagra be available

a http://sildenafilneu.com# generic viagra india
how to take viagra
sildenafil citrate 100mg lowest price
canada viagra
sildenafil coupons [url=http://sildenafilneu.com#]sildenafil citrate generic viagra 100mg[/url]

a http://sildenafilneu.com# generic viagra india
how to take viagra
sildenafil citrate 100mg lowest price
canada viagra
sildenafil coupons [url=http://sildenafilneu.com#]sildenafil citrate generic viagra 100mg[/url]

e http://cialisneu.com# cialis online
purchasing cialis in mexico
cialis coupon
cialis coupon [url=http://cialisneu.com#]rsa anti-fraud command center[/url]

e http://cialisneu.com# cialis online
purchasing cialis in mexico
cialis coupon
cialis coupon [url=http://cialisneu.com#]rsa anti-fraud command center[/url]

axuhio tm eye ltd

axuhio tm eye ltd

Pages

Add new comment