Running javascript in a dynamically generated iframe

I have a tool that generates some html + javascript code, it shows the code in a single div, and then shows a preview of the code, as shown in the iframe.

Here is a quick fiddle that shows this ... http://jsfiddle.net/Zv4zU/

But javascript inside the iframe never starts, although if you check the iframe, javascript is definitely there. How can I run javascript on my own?

+3
source share
2 answers

See jsfiddle for a solution!

Edit: As Jonathan pointed out, I should also put the code here:

var html = "html code <script type='text/javascript'>alert('OK');<\/script>";
$("#myFrame").contents().find("body").html(html);
+1
source

script, JS iframe:

var script = "alert('OK');document.body.innerHTML = 'hello!'";
myFrame.get(0).contentWindow.eval(script);

. jsfeedle

0

All Articles