1

I am building a simple online HTML IDE where students can type HTML into a codemirror editor and then when a button is clicked, the HTML is placed in an iframe to render the page.

My code in question:

function runit() { 
   var prog = myCodeMirror.getValue(); 
   var mypre = document.getElementById("output");
   mypre.contentDocument.body.outerHTML = prog; 
 
}

prog is the html from the editor, mypre is the iframe.

All html placed within <body> tags is correctly executed in the iframe. All css code placed within <style> tags is correctly executed in the iframe. However, any javascript placed within <script> tags is not executed in the iframe.

How can I get <script> tag contents to be correctly executed in the iframe?

1

1 Answer 1

1

This may help:

const runit = () => document.getElementById("output").srcdoc = myCodeMirror.getValue();

We use the property srcdoc to change the content in the iframe as HTML

Sign up to request clarification or add additional context in comments.

1 Comment

I recommend u using let instead of var and if the variable is not used two or more times you don't need to declare it and just use its value instead :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.