Monthly ArchiveSeptember 2007
Howto & Web & Tips & Tricks & Marketing 14 Sep 2007 02:14 am
AdSense banners based on visitor screen resolution
This may be obvious to some with more experience but this little trick has served me well so I thought I would share it.
Imagine you want a leaderboard AdSense ad somewhere in the body of your page which also sports a left menu. For those visitors using a screen resolution of 800×600 the 768 wide leaderboard stretches the page beyond its bounds, and the browser offers the annoying horizontal scrollbar.
Happily, there’s a simple JavaScript solution:
if(screen.width< =800){
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
} else {
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
}
Visitors with a screen width of 800 pixels or less will be shown the standard banner ad and visitors with larger screen width are shown the larger leaderboard ad. Of course you should remove old references to google_ad_width, google_ad_height and google_ad_format and replace it with the code.
I hope this helps someone.
DW