views:

52

answers:

1

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.

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(
    '<div style="border:solid 3px red;">'
    + '<sc' + 'ript src="' + url + '">'
    + '</sc' + 'ript>'
    + 'The ad should be inside the same box as this text.</div>'
  );
}

adsense(160, 600, 5133629129);

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. You can see the difference in this pic: http://i.imgur.com/ZclcG.png (top window is Chrome, bottom one is IE).

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?

A: 

I placed the div tag outside and it worked .. looks like its a problem with the way IE handles javascript..

<div style="border:solid 3px red">
<script type="text/javascript" charset="utf-8">

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(
     '<sc' + 'ript src="' + url + '">'
    + '</sc' + 'ript>'
    + 'The ad should be inside the same box as this text.'
  );
}

adsense(160, 600, 5133629129);


</script>
</div>
dejavu
Thanks, but unfortunately as I said I am unable to have straight HTML like you are using for the <div> here. The code needs to create all the elements it uses.
Bemmu