title: Can't write to dynamic iframe using jQuery
body: <p>My goal is to dynamically create an iframe and write ad JavaScript into it using jQuery (e.g. Google AdSense script). My code works on Chrome, but fails intermittently in Firefox i.e. sometimes the ad script runs and renders the ad, and other times it doesn't. When it doesn't work, the script code itself shows up in the iframe.</p>
  
  <p>My <strong>guess</strong> is these intermittent failures occur because the iframe is not ready by the time I write to it. I have tried various iterations of <em>iframe_html</em> (my name for the function which is supposed to wait for the iframe to be ready), but no luck. Any help appreciated!</p>
  
  <p>PS: I have read various threads (e.g. <a href="https://stackoverflow.com/questions/205087/jquery-ready-in-a-dynamically-inserted-iframe">jQuery .ready in a dynamically inserted iframe</a>). Just letting everyone know that I've done my research on this, but I'm stuck :)</p>
  
  <p><strong>Iteration 1:</strong></p>
  
  <pre><code>function iframe_html(html){
   $('&lt;iframe name ="myiframe" id="myiframe"/&gt;').appendTo('#maindiv');
   $('#myiframe').load(
     function(){
      $('#myiframe').ready( function(){
       var d = $("#myiframe")[0].contentWindow.document;
       d.open();
       d.close();
       d.write(html);
       });
     }
   );
  };
  </code></pre>
  
  <p><strong>Iteration 2:</strong></p>
  
  <pre><code>function iframe_html(html){
   $('&lt;iframe id="myiframe"/&gt;').appendTo('#maindiv').ready(
     function(){
      $("#myiframe").contents().get(0).write(html);
     }
   ); 
  };
  </code></pre>