I have been struggling with the problem of how to show different adsense ads on different layouts for a while now and am just curious on how others have tackled the problem.

I was planning on loading different blocks with ajax depending on the layout, and started writing a module, but have since realized that adsense ads don't work when added to the page via ajax.

The best solution I have come up with is something like this:

<script type="text/javascript"><!--
google_ad_client = "ca-pub-XXXXXXXXX";
/* top */
if ($(window).width()<740 ){
     google_ad_slot = "XXXXXX";
     google_ad_width = 320;
     google_ad_height = 50;
}else{
     google_ad_slot = "XXXXXXX";
     google_ad_width = 728;
     google_ad_height = 90;
}
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">

Does anyone have any alternative solutions or implemented the functionality in a different way?