title: How in insert dynamic JavaScript tag into iframe
body: <p><strong>FINAL UPDATE: The second code block is working--I just miss-typed the URL.</strong></p>
  
  <p>I'm trying to place a dynamically created script into an iframe. I can access the head of the iframe, but it doesn't seem to accept the append request. The console log call returns the head tag, and the following append of text into the body works fine. I've also tried appending the script tag to the body. (note: the script tag is not on the same domain, so I've avoided using getScript or ajax).</p>
  
  <pre><code>$(document).ready(function() {
    $('&lt;iframe&gt;&lt;/iframe&gt;', {
        name:"contentFrame"
        ,id:"contentFrame"
    }).css("height", 300).css("width", 500).load(function() {
        var script   = document.createElement("script");
        script.type  = "text/javascript";
        script.src   = "//cross-domain.com/script.js";
        console.log($(this).contents().find("head")); // outputs: [&lt;head&gt;&lt;/head&gt;]
        $(this).contents().find("head").append(script); // doesn't work
        $(this).contents().find("body").append(script); // doesn't work
        $(this).contents().find("body").append("Test"); // works
    }).appendTo("#content");
  });
  </code></pre>
  
  <p>Here is the new version that successfully appends the script based upon the suggestion below, but the script doesn't evaluate (as tested by a simple alert). Plus, another oddity: the "working" icon displays for about 15 seconds and the status (in chrome) says "retrieving..." Eventually it stops, but nothing happens.</p>
  
  <pre><code>    $(document).ready(function() {
    $('&lt;iframe&gt;&lt;/iframe&gt;', {
        name:"contentFrame"
        ,id:"contentFrame"
    }).css("height", 300).css("width", 500).load(function() {
        var script   = document.createElement("script");
        script.type  = "text/javascript";
        script.src   = "//cross-domain.com/script.js";
        console.log($(this).contents().find("head"));
        $(this).contents().find("head")[0].appendChild(script);
        $(this).contents().find("body").append("Test");
    }).appendTo("#content");
  });
  </code></pre>
  
  <p>Update: When the cursor icon finally finishes working (about 10 seconds, which is odd because it's a local server with one line of code), I get this message in the Chrome console (it's in all red with a red circle "X" icon next to it):</p>
  
  <pre><code>GET http://cross-domain.com/script.js 
  (anonymous function)temp.html:16
  jQuery.event.dispatchjquery-1.7.1.js:3261
  jQuery.event.add.elemData.handle.eventHandlejquery-1.7.1.js:2880
  jQuery.fn.extend.appendjquery-1.7.1.js:5771
  jQuery.fn.extend.domManipjquery-1.7.1.js:5973
  jQuery.fn.extend.appendjquery-1.7.1.js:5769
  jQuery.each.jQuery.fn.(anonymous function)jquery-1.7.1.js:6162
  (anonymous function)temp.html:18
  jQuery.Callbacks.firejquery-1.7.1.js:1049
  jQuery.Callbacks.self.fireWithjquery-1.7.1.js:1167
  jQuery.extend.readyjquery-1.7.1.js:435
  DOMContentLoaded
  </code></pre>