How do I use JavaScript to load/append jQuery libraries inside an HTML5 iframe?
<p>I have created a simple code editor using Angular JS &amp; old fashioned JavaScript and am now at the stage where I'd like to add an  code preview facility.</p>

<p>Here's the code I used to dynamically create the iframe, add it to DOM and populate with the elements I need to make it work:</p>

<pre><code>...
function updatePreview() {
    var iFrame, doc, styleElm, jQueryElem, styles, javascript;
    var container, $head, $body;
    var $jsRequirements = [], addditionalJS = [], $cssRequirements = [];

    //set base requirements for iframe element
    $jsRequirements = [
        'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js',
        'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js',
    ];

    $additionalJS = [
        'js/libs/main.js',
        'js/libs/plugin.slideshow.js'
    ];

    $cssRequirements = [
        'c/theme/jquery-ui-1.8.23.custom.css'
    ];

    //set up a new HTML5 iFrame with basic styling
    iFrame = document.createElement('iframe');
    iFrame.className = "previewPane";
    iFrame.style.height = "100%";
    iFrame.style.width = "100%";

    //append iFrame to previewContainer
    container = document.getElementById('previewPanel');
    container.appendChild(iFrame);

    //init main iFrame content for manipulation
    doc =  iFrame.contentDocument ||  iFrame.contentWindow.document;

    // init the inline style container element
    styleElm = doc.createElement('style');
    styleElm.setAttribute('rel','stylesheet');
    styleElm.setAttribute('type','text/css');
    styleElm.setAttribute('media', 'screen');

    //grab css from CSS code pane
    styles = cssCode.getValue();

    //grab JS from the JS code pane
    javascript = jsCode.getValue();

    //write main HTML code
    doc.open();
    doc.write(htmlCode.getValue());
    doc.close();

    //must add any JS &amp; styling *after* initial html code dump
    $head = doc.getElementsByTagName('head')[0];
    $body = doc.getElementsByTagName('body')[0];

    //init script element and populate 
    //with local jQuery variable declaration 
    //for iFrame script execution
    jQueryElem = doc.createElement('script');
    jQueryElem.setAttribute('type','text/javascript');
    //append the JS from the jsPanel to the same element
    jQueryElem.appendChild(document.createTextNode(javascript));

    //now the javascript
    setupJSReqs($jsRequirements, doc, $head);
    setupJSReqs($additionalJS, doc, $body);

    styleElm.appendChild(document.createTextNode(styles));
    $head.appendChild(styleElm);
    $body.appendChild(jQueryElem);

}

    setTimeout(updatePreview, 300);
});

}

function setupJSReqs(JSArray, container, target) {
    for (var x in JSArray) {
        var jsElem = container.createElement('script');
        jsElem.setAttribute('type','text/javascript');
        jsElem.setAttribute('src', JSArray[x]);
        target.appendChild(jsElem);
    }
}
</code></pre>

<p>This code works fine visually (i.e. chrome dev tools show all the elements in the correct place) but I get an error:</p>

<pre><code>**Uncaught ReferenceError: $ is not defined**
</code></pre>

<p>I could be wrong but I'm guessing the dom is not pulling in the iframe jQuery library correctly.  I can get round this by something like var $ = parent.$ but it's not and ideal solution.</p>

<p>Any suggestions to fix the error??</p>

<p>Thanks</p>
