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

best online casino slots for real money casino bonus codes online gambling casino | [url=https://onlinecasinoplay.us.org/]empire city online casino[/url] [url=https://onlinecasinoplay.us.org/]best online casino[/url]

best online casino slots for real money casino bonus codes online gambling casino | [url=https://onlinecasinoplay.us.org/]empire city online casino[/url] [url=https://onlinecasinoplay.us.org/]best online casino[/url]

free casino games casino slots bovada casino online casino | https://onlinecasinoplay.us.org/ - vegas world casino games https://onlinecasinoplay.us.org/ - doubledown casino

free casino games vegas world virgin online casino lady luck no deposit casino | https://onlinecasinoplay.us.org/ - free casino slot games https://onlinecasinoplay.us.org/ - play casino

http://alsannat.salik.net.sa/?option=com_k2&view=itemlist&task=user&id=7...
http://daysofcode.io/wiki/index.php/User:NoeFay726054803
http://ecars.inoe.ro/wiki/index.php/User:ErvinSturt59
http://only-the-facts.com/index.php/User:OttoAyala3867
http://scripts.mit.edu/~jweiss/lets-eat/index.php?title=User:MichellSaenz
http://only-the-facts.com/index.php/User:Gilberto8017
http://www.neubis.org/index.php?title=User:KalaMacias420
http://www2.compassis.com/mediawiki-dev/index.php/User:NoellaRou0004
https://www.natal.wiki.br/index.php/User:CindySpowers
https://www.natal.wiki.br/index.php/User:JeremyRuby9
http://scripts.mit.edu/~jweiss/lets-eat/index.php?title=User:MoniqueRuiz7
http://daysofcode.io/wiki/index.php/User:Lettie95U19071
http://only-the-facts.com/index.php/User:Janelle0499
http://www2.compassis.com/mediawiki-dev/index.php/User:FranciscaPrince
http://ecars.inoe.ro/wiki/index.php/User:EmmettW9697678
http://www2.compassis.com/mediawiki-dev/index.php/User:KristalKinsela9
http://only-the-facts.com/index.php/User:MindyTeel0
http://bodamerlab.org/wiki/index.php?title=User:TeshaRayburn651
http://bodamerlab.org/wiki/index.php?title=User:CornellHaviland
http://bodamerlab.org/wiki/index.php?title=User:SheliaCornett7
http://www.neubis.org/index.php?title=User:GaleCollits1512
http://bodamerlab.org/wiki/index.php?title=User:WadeChristianson
http://scripts.mit.edu/~jweiss/lets-eat/index.php?title=User:Winifred62O
http://scripts.mit.edu/~jweiss/lets-eat/index.php?title=User:ReinaTitus5253
http://driftpedia.com/wiki/User:AdeleThigpen401

borgata online casino free casino slot games online casino real money casino online | https://onlinecasinoplay.us.org/ - foxwoods online casino https://onlinecasinoplay.us.org/ - online casinos

borgata online casino free casino slot games online casino real money casino online | https://onlinecasinoplay.us.org/ - foxwoods online casino https://onlinecasinoplay.us.org/ - online casinos

zone online casino online casinos for us players online casino bonus firekeepers casino | https://onlinecasinoplay.us.org/ - hypercasinos https://onlinecasinoplay.us.org/ - real money casino

slots for real money doubledown casino free casino games slotomania doubledown casino | https://onlinecasinoplay.us.org/ - big fish casino https://onlinecasinoplay.us.org/ - lady luck

slots for real money doubledown casino free casino games slotomania doubledown casino | https://onlinecasinoplay.us.org/ - big fish casino https://onlinecasinoplay.us.org/ - lady luck

mgm online casino casino games free online free casino games no download | https://onlinecasino888.us.org/ - casino games

borgata online casino online casino bonus zone online casino games | https://onlinecasino888.us.org/ - free casino games slot machines

big fish casino borgata online casino zone online casino games | https://onlinecasino888.us.org/ - online casino gambling

online gambling gsn casino foxwoods online casino | https://onlinecasino888.us.org/ - lady luck

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

casino games free online online casinos best online casinos | https://onlinecasino888.us.org/ - zone online casino games

casino games online free casino games no download tropicana online casino | https://onlinecasino888.us.org/ - parx online casino

casino games online free casino games no download tropicana online casino | https://onlinecasino888.us.org/ - parx online casino

hypercasinos casino games slots free casino slots | https://onlinecasino888.us.org/ - free casino games vegas world

foxwoods online casino casino games free online casino blackjack | https://onlinecasino888.us.org/ - online gambling

chumba casino high 5 casino casino play | https://onlinecasino888.us.org/ - hollywood casino

chumba casino high 5 casino casino play | https://onlinecasino888.us.org/ - hollywood casino

Absolutely NEW update of captcha recognition software "XRumer 16.0 + XEvil":
captcha breaking of Google (ReCaptcha-2 and ReCaptcha-3), Facebook, BitFinex, Bing, Hotmail, SolveMedia, Yandex,
and more than 8400 another 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 software: iMacros, XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other programms.

Interested? You can find a lot of introducing videos about XEvil in YouTube.

FREE DEMO AVAILABLE!

Good luck ;)

http://XEvil.net/

Absolutely NEW update of captcha recognition software "XRumer 16.0 + XEvil":
captcha breaking of Google (ReCaptcha-2 and ReCaptcha-3), Facebook, BitFinex, Bing, Hotmail, SolveMedia, Yandex,
and more than 8400 another 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 software: iMacros, XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other programms.

Interested? You can find a lot of introducing videos about XEvil in YouTube.

FREE DEMO AVAILABLE!

Good luck ;)

http://XEvil.net/

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

hyper casinos gsn casino games casino play | https://onlinecasino888.us.org/ - casino games free online

online casino gambling casino slots chumba casino | https://onlinecasino888.us.org/ - casino online slots

online casinos play casino high 5 casino | https://onlinecasino2018.us.org/ - play casino

real money casino casino bonus real money casino | https://onlinecasino2018.us.org/ - big fish casino

instant loans advanced payday loans debt consolidation companies 100 guaranteed payday loans

instant loans advanced payday loans debt consolidation companies 100 guaranteed payday loans

online casino games virgin online casino casino games slots free | https://onlinecasino2018.us.org/ - big fish casino

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

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

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

Pages

Add new comment