Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5966971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:52:32+00:00 2026-05-22T19:52:32+00:00

I have an OpenSocial application, and wanted to try AdSense in it. I can

  • 0

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.

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:52:33+00:00Added an answer on May 22, 2026 at 7:52 pm

    It’s too late now, obviously, but I had the same issue and I asked it on StackOverflow (going to be posting the long form answer soon on there too) but the only thing you can really do is load it in an iframe.

    Step 1
    Create an HTML file on your server with your adsense code like:

    <script type="text/javascript"><!--
    google_ad_client = "ca-pub-xxxxxxxxxxxx";
    /* Some Ad */
    google_ad_slot = "xxxxxxxxx";
    google_ad_width = 468;
    google_ad_height = 60;
    //--></script>
    <script type="text/javascript"
      src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    

    Step 2

    In that same HTML file you just created, at the top, add:

    <style> body {padding:0;margin:0;} </style>
    

    This will make sure there is no spacing in the iframe and the ad code fits snuggly against the border of the iframe.

    Step 3
    Dynamically add the iframe like:

    var insertAdsenseIframe = function(src,width,height){
      var iframe = document.createElement('iframe');
      iframe.setAttribute("frameborder","0");
      iframe.setAttribute("src", src);
      iframe.setAttribute("width", width);
      iframe.setAttribute("height", height);
      return iframe; //Return it so you can use whatever method you want to insert/append
    }
    
    var myIframe = insertAdsenseIframe('http://oscargodson.com/dev/scriptTest/cleariframe.html','468px','60px');
    
    document.getElementById('test').appendChild(myIframe);
    

    The JavaScript is just a sample, but works. I never tested in IE or anything. This is just to get you started.

    Demo

    Because it’d be against the TOS to post this on JSBin then StackOverflow I just created a div in the shape of an ad, but if you put the ad in it’ll work.

    See: http://jsbin.com/evoqi6/
    Hack: http://jsbin.com/evoqi6/edit

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have follwing JSON: {mykey:[{name:Jak,interests:movies}]} and following opensocial app code: <script type=text/os-template require=mykey> <ul>
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
I have got the list of my friends and i can traverse through the
really sorry about being totally thick today but I have forgotten how to do
Have converted devise new session from erb to Haml but doens't work, this is
I'm finding myself writing a lot of spaghetti in Javascript when I have to
Have one Doubt In MVC Architecture we can able to pass data from Controller
Have searched for the answer but no joy, so here goes... I'm working on
Could I have an example of communication between two OpenSocial gadgets using the gadgets.rpc
Have anyone used Redmine Documentor which lets you convert PHP to HTML to Redmine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.