title: Why is this Javascript code for inserting an Adsense ad not working?
body: <p>Starting with <a href="https://stackoverflow.com/q/11243398/184108">advice from this question</a>, I did some searching and found some code on <a href="http://www.webmasterworld.com/google_adsense/4397994.htm" rel="nofollow noreferrer">this web site</a>, which is intended to insert a Google Adsense ad into a particular spot on my web page, <em>after</em> the page is loaded. In my case, I do some checking in Javascript, send JSON data back to my server, and then based on the response I get from that JSON, I determine whether or not to show the Google Adsense ad.</p>
  
  <p>Since I got this code from the web, and didn't write it myself, there's a lot of it I don't get, and in some places I had to fill out details based on what I thought was required, but I was guessing a bit. This is how the code I'm currently using looks:</p>
  
  <pre><code>if(userStatus.status == 0)
  {
      console.log("google ad should show");
  
      window["google_ad_client"] = 'ca-pub-0000000000000000';
      window["google_ad_slot"]  = "0000000000";
      window["google_ad_width"]  = 320;
      window["google_ad_height"]  = 50;
  
      window.adcontainer = document.getElementById('google-ad');
      window.adhtml = '';
  
      function mywrite(html)
      {
          adhtml += html;
          if(html == '&lt;/iframe&gt;')
          {
              adcontainer.innerHTML = adhtml;
          }
      };
  
      document.write_ = document.write;
      document.write = mywrite;
  
      script = document.createElement('script');
      script.src='http://pagead2.googlesyndication.com/pagead/show_ads.js';
      script.type='text/javascript';
      document.body.appendChild(script);
  }
  </code></pre>
  
  <p>It seems that it is almost working.</p>
  
  <p>When I view my page in Firefox with Firebug enabled, I see that my console log message is being displayed, so I know I've got through all my conditionals.</p>
  
  <p>I've also tried running the ad with the code written directly into the HTML, just to confirm that Google has accepted my web site and that they will display ads. When I do that, the ads display fine, so I believe there's nothing wrong with my Asense account.</p>
  
  <p>However, while the call to <code>http://pagead2.googlesyndication.com/pagead/show_ads.js</code> is being appended to the end of the page, it seems that the ad parameters are not being written into the <code>google-ad</code> DIV as intended.</p>
  
  <p>Thus, the ad is not displaying.</p>
  
  <p>Where is my code going wrong?</p>