title: Failed to write document on Dynamic Async Iframe
body: <p>I'm using <a href="http://www.aaronpeters.nl/blog/iframe-loading-techniques-performance" rel="nofollow">Dynamic Async Iframe</a> to load one of our ads and I got this warning and the ads won't load. </p>
  
  <blockquote>
    <p>Failed to execute 'write' on 'Document': It isn't possible to write
    into a document from an asynchronously-loaded external script unless
    it is explicitly opened.</p>
  </blockquote>
  
  <p>This is the code I use to load the ad</p>
  
  <pre><code>var adSrc = '&lt;?php echo esc_url( $src ); ?&gt;';
          var escapedAdSrc = adSrc.replace(/\//g,'\\/');
          var adWidth = &lt;?php echo $width; ?&gt;;
          var adHeight = &lt;?php echo $height; ?&gt;;
          var theiFrame = document.createElement('iframe');
          theiFrame.frameBorder = 0;
          var iFrame = jQuery('#&lt;?php echo $id; ?&gt;').append(theiFrame);
  
          var adDoc = theiFrame.contentWindow.document;
          theiFrame.style.cssText = 'width:' + adWidth + 'px;height:' + adHeight +'px;';
  
          //adDoc.open().write('&lt;body onload="location.href=\'' + escapedAdSrc + '\'"&gt;');
          adDoc.open().write('&lt;body onload="' + 
              'var d = document;d.getElementsByTagName(\'head\')[0].' + 
              'appendChild(d.createElement(\'script\')).src' + 
              '=\'' + escapedAdSrc + '\'"&gt;');
          adDoc.close();
  </code></pre>
  
  <p>I believe partly because ad script has <code>document.write</code> in the script that's why I got this error. But I'm not sure how to solve the problem since I the script is third party script. </p>