title: Dynamic AdSense units
body: <p>I have an OpenSocial application, and wanted to try AdSense in it. I can easily run Javascript, but cannot include HTML snippets directly. For this reason I am trying to come up with a script that would create a DIV that contains the AdSense ad.</p>
  
  <pre><code>function adsense(w, h, slot) {
  google_ad_client = "pub-4815352041522054";
  google_ad_slot = slot;
  google_ad_width = w;
  google_ad_height = h;
  
  var url = "http://pagead2.googlesyndication.com/pagead/show_ads.js";    
  document.write(
      '&lt;div style="border:solid 3px red;"&gt;'
      + '&lt;sc' + 'ript src="' + url + '"&gt;'
      + '&lt;/sc' + 'ript&gt;'
      + 'The ad should be inside the same box as this text.&lt;/div&gt;'
    );
  }
  
  adsense(160, 600, 5133629129);
  </code></pre>
  
  <p>The code above will write a div with a red border and place the AdSense unit inside of it. This works fine on Chrome, but on IE the referenced AdSense code seems to run after the div has already closed.</p>
  
  <p>I'd like to be able to create AdSense units dynamically from code (for example for A/B testing purposes) and reposition them as required. Any ideas on how to get this to work?</p>