How to load ads last on page

22. Januar 2010

Last week I had a big problem with one of my websites taking part in an adnetwork. The ads are provided via javascript by an adserver of the network, but this day the adserver seemed to have a problem and my whole site was very slow or couldn’t even be loaded, because the ads were positioned relatively inside the html-code. Of course it is always a good idea to execute javascript code at the end of the page. But in some cases you just can not position the ads absolutely. So I found a workaround to load ads last on a webpage even though they are positioned relatively. And here is the code:
You can put the following code in your html-code at any place where your ad shall appear:

<div id="ad" style=""></div>

You can position the div with the style element as usual.
And then at the end of your page before the body-element you put the following code:

<div id="adpush" style="display:block;">
!Here comes you ad-tag!
</div>
<script type="text/javascript">
var adpush = document.getElementById('adpush');
document.getElementById("ad").appendChild(adpush);
</script>

The ad will now be pushed into the div “ad” when your ad-tag is executed. So now the whole page will be loaded, as if no ads were included. And then, when the adcode is loaded, they will pop up in the correct position. Following content after the ad is then moved below the ad. I tested this code in several browsers (IE6, IE7, IE8, Firefox and Opera) and no problems appeared. You could probably use it with Google Adsense ads, too. But so far at least on my sites they were fast enough.
So in my opinion it is a good way to speed up your website. If I could help you with this solution, don’t hesitate to digg it…

(’DiggThis’)

Einen Kommentar abgeben:

Weitere Beiträge zum Thema: