Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8421115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:03:28+00:00 2026-06-10T03:03:28+00:00

I have created a simple code editor using Angular JS & old fashioned JavaScript

  • 0

I have created a simple code editor using Angular JS & old fashioned JavaScript and am now at the stage where I’d like to add an code preview facility.

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:

...
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 & 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);
    }
}

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

**Uncaught ReferenceError: $ is not defined**

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.

Any suggestions to fix the error??

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:03:29+00:00Added an answer on June 10, 2026 at 3:03 am

    You are dynamically appending the <script> elements with DOM manipulation. This means that the external scripts are loaded asynchronous – and will be executed after the (later-inserted) inline script.

    To work around this, you can:

    • Hook on the load events of the external resources and insert the inline script after all of them are loaded
    • Use doc.write(…) for the whole page, i.e. insert the script tags into your html string. Then the script will load and execute synchronously.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created blank C#/XAML Windows 8 application. Add simple XAML code: <Page x:Class=Blank.MainPage
I have created the simple web service. Code: [ServiceContract] public interface ITsdxService { [OperationContract]
I have created a CPTTradingRangePlot using the up-to-date sample code as an example. The
Could someone help me on this, I have created simple web services using axis2
I'm trying to create a simple Word add-in. I have created a Word 2010
I want to create an editor in C++ using SDL & OpenGL and have
I have tried to create a simple controller/model (based on the samples code of
I have the following code which creates a simple window with two buttons which
I have used following code to create a simple PDF file. It executes fine
I have a simple python curses code that creates a subwindow. However, in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.