Serve the right width Google Adsense ad

On Media UK, which uses a fluid layout, I serve a 468x60 ad banner at the top of most pages. Except it would be nice, wouldn't it, to serve a larger 728x90 adbanner if the user's browser is wide enough.

This little script does the job.

You'll need to, first, set up two Google AdSense ads: one for a 468x60, one for a 728x90. Then, you need to copy the numbers into the script below for google_ad_client, and the two google_ad_slots. You'll find the correct numbers for you by getting the code for the relevant ad slots in Google AdSense itself.

In this case, the main DIV ID is "maincontent", which is the full width of the main content of the page. This script simply checks the actual width of 'maincontent', and, if it's bigger than 728, shoves a leaderboard in there instead of a small banner instead.

<script type="text/javascript"><!--
google_ad_client = "ca-pub-527xxxxxxxxxxxx";
if ( (document.getElementById('maincontent').offsetWidth) > 728) {
/* 728x90 */
google_ad_slot = "072xxxxxxx";
google_ad_width = 728;
google_ad_height = 90;
} else {
/* 468 x 60 */
google_ad_slot = "270xxxxxxx";
google_ad_width = 468;
google_ad_height = 60;
}
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

Download this code