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

hollywood casino play4fun choctaw casino gsn casino on facebook blue chip casino | https://realmoneycasino.ooo/ - jackpot party casino facebook https://realmoneycasino.ooo/ - doubledown casino facebook

tropicana online casino casino slots casino games free online empire city online casino | https://onlinecasinoplay.us.org/ - free vegas casino games https://onlinecasinoplay.us.org/ - zone online casino games

tropicana online casino casino slots casino games free online empire city online casino | https://onlinecasinoplay.us.org/ - free vegas casino games https://onlinecasinoplay.us.org/ - zone online casino games

slots games free slots free games slots online free pch slots | https://slotgames.icu/ - brian christopher slots https://slotgames.icu/ - free slots hollywood

free casino slot games caesars online casino chumba casino hallmark casino online | https://realmoneycasino.ooo/ - free casino games slotomania https://realmoneycasino.ooo/ - riverwind casino

free casino slot games caesars online casino chumba casino hallmark casino online | https://realmoneycasino.ooo/ - free casino games slotomania https://realmoneycasino.ooo/ - riverwind casino

free casino slots charlestown races and slots free online slots no download house of fun free slots | https://slotsrealmoney.icu/ - free casino slots https://slotsrealmoney.icu/ - slots lounge

free casino slots charlestown races and slots free online slots no download house of fun free slots | https://slotsrealmoney.icu/ - free casino slots https://slotsrealmoney.icu/ - slots lounge

gambling sites oxford casino turning stone casino casinos in iowa | https://playonlinecasino.icu/ - free casino games slot machines https://playonlinecasino.icu/ - borgata hotel casino

virgin online casino empire city online casino foxwoods online casino online casino gambling | https://onlinecasinoplay.us.org/ - best online casino https://onlinecasinoplay.us.org/ - empire city online casino

virgin online casino empire city online casino foxwoods online casino online casino gambling | https://onlinecasinoplay.us.org/ - best online casino https://onlinecasinoplay.us.org/ - empire city online casino

pala casino seneca niagara casino casinos near me slotomania slot machines | https://realmoneycasino.ooo/ - doubleu casino on facebook https://realmoneycasino.ooo/ - rock n cash casino slots

pala casino seneca niagara casino casinos near me slotomania slot machines | https://realmoneycasino.ooo/ - doubleu casino on facebook https://realmoneycasino.ooo/ - rock n cash casino slots

mountaineer casino spirit mountain casino online slot machines codeshareonline doubledown casino | https://playcasinoslots.ooo/ - online casinos https://playcasinoslots.ooo/ - bonus casino

hot shot casino slots online casino slots no download free slots online no download no registration caesars slots free casino | https://slotgames.icu/ - las vegas free slots https://slotgames.icu/ - hollywood casino online slots

hot shot casino slots online casino slots no download free slots online no download no registration caesars slots free casino | https://slotgames.icu/ - las vegas free slots https://slotgames.icu/ - hollywood casino online slots

vegas world free games online slots gold fish casino slots konami free slots pch slots tournament | [url=https://slotsrealmoney.icu/]free online slots vegas world[/url] [url=https://slotsrealmoney.icu/]hollywood casino online slots[/url]

vegas world free games online slots gold fish casino slots konami free slots pch slots tournament | [url=https://slotsrealmoney.icu/]free online slots vegas world[/url] [url=https://slotsrealmoney.icu/]hollywood casino online slots[/url]

bovada blackjack real money casino posh casino online casino bonus codes | https://playonlinecasino.icu/ - doubleu casino on facebook https://playonlinecasino.icu/ - pala casino online

bovada blackjack real money casino posh casino online casino bonus codes | https://playonlinecasino.icu/ - doubleu casino on facebook https://playonlinecasino.icu/ - pala casino online

gambling sites paradise casino free casino games slotomania zone online casino games | https://realmoneycasino.ooo/ - winstar casino https://realmoneycasino.ooo/ - parx casino

gambling sites paradise casino free casino games slotomania zone online casino games | https://realmoneycasino.ooo/ - winstar casino https://realmoneycasino.ooo/ - parx casino

casino slot red rock casino winstar casino parx casino | https://playcasinoslots.ooo/ - plainridge casino https://playcasinoslots.ooo/ - plainridge casino

las vegas free slots free online slots free games online slots heart of vegas free slots | https://slotgames.icu/ - vegas world free slots games https://slotgames.icu/ - goldfish casino slots free

las vegas free slots free online slots free games online slots heart of vegas free slots | https://slotgames.icu/ - vegas world free slots games https://slotgames.icu/ - goldfish casino slots free

online casino hyper casinos play casino zone online casino | https://onlinecasinoplay.us.org/ - free casino games no download https://onlinecasinoplay.us.org/ - vegas world casino games

belterra casino island view casino free online casino meadows casino | https://playonlinecasino.icu/ - san manuel casino https://playonlinecasino.icu/ - casinos in iowa

belterra casino island view casino free online casino meadows casino | https://playonlinecasino.icu/ - san manuel casino https://playonlinecasino.icu/ - casinos in iowa

meadows casino ilani casino viejas casino jackpot party casino facebook | https://onlinecasinoo.icu/ - best online casinos https://onlinecasinoo.icu/ - online casino reviews

meadows casino ilani casino viejas casino jackpot party casino facebook | https://onlinecasinoo.icu/ - best online casinos https://onlinecasinoo.icu/ - online casino reviews

clearwater casino ilani casino potawatomi casino best online casinos | https://realmoneycasino.ooo/ - hollywood casino online slots https://realmoneycasino.ooo/ - cafe casino online

clearwater casino ilani casino potawatomi casino best online casinos | https://realmoneycasino.ooo/ - hollywood casino online slots https://realmoneycasino.ooo/ - cafe casino online

bigfish casino online games pechanga casino usa online casino gsn casino slots | https://playcasinoslots.ooo/ - seneca niagara casino https://playcasinoslots.ooo/ - san manuel casino

bigfish casino online games pechanga casino usa online casino gsn casino slots | https://playcasinoslots.ooo/ - seneca niagara casino https://playcasinoslots.ooo/ - san manuel casino

online slots heart of vegas free slots free slot machine games hot shot casino slots | https://slotgames.icu/ - free slots hollywood https://slotgames.icu/ - free slots no download no registration

online slots heart of vegas free slots free slot machine games hot shot casino slots | https://slotgames.icu/ - free slots hollywood https://slotgames.icu/ - free slots no download no registration

spirit mountain casino gun lake casino turning stone casino online gambling | https://onlinecasinoo.icu/ - seneca niagara casino https://onlinecasinoo.icu/ - online casino slots

spirit mountain casino gun lake casino turning stone casino online gambling | https://onlinecasinoo.icu/ - seneca niagara casino https://onlinecasinoo.icu/ - online casino slots

lady luck online casinos casino online slots free casino games slot machines | https://onlinecasinoplay.us.org/ - tropicana online casino https://onlinecasinoplay.us.org/ - online casino gambling

riverwind casino online gambling choctaw casino durant oklahoma lady luck | https://playcasinoslots.ooo/ - jackpot party casino https://playcasinoslots.ooo/ - online slot machines

riverwind casino online gambling choctaw casino durant oklahoma lady luck | https://playcasinoslots.ooo/ - jackpot party casino https://playcasinoslots.ooo/ - online slot machines

wheel of fortune slots simslots free slots free slots games vegas world free slots | https://slotgames.icu/ - play free slots for fun https://slotgames.icu/ - myvegas slots

wheel of fortune slots simslots free slots free slots games vegas world free slots | https://slotgames.icu/ - play free slots for fun https://slotgames.icu/ - myvegas slots

prairie meadows casino virgin casino online online casino bonus posh casino online | https://onlinecasinoo.icu/ - pala casino online https://onlinecasinoo.icu/ - casino game

casino games online turning stone online casino free casino games online doubleu casino on facebook | https://realmoneycasino.ooo/ - red rock casino https://realmoneycasino.ooo/ - foxwoods casino online slots

casino games online turning stone online casino free casino games online doubleu casino on facebook | https://realmoneycasino.ooo/ - red rock casino https://realmoneycasino.ooo/ - foxwoods casino online slots

free games online slots slot machine games free buffalo slots goldfish slots | https://slotsrealmoney.icu/ - slots free https://slotsrealmoney.icu/ - online slot games

free games online slots slot machine games free buffalo slots goldfish slots | https://slotsrealmoney.icu/ - slots free https://slotsrealmoney.icu/ - online slot games

tropicana online casino doubledown casino las vegas casinos winstar world casino | https://onlinecasinoplay.us.org/ - online gambling https://onlinecasinoplay.us.org/ - casino games

gsn casino big fish casino gun lake casino sugarhouse casino online | https://onlinecasinoo.icu/ - free casino games sun moon https://onlinecasinoo.icu/ - free casino

Pages

Add new comment