Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run javascript in a dynamically created iframe

I have a tool which generates some html+javascript code, it shows the code in one div and then shows a preview of what the code displays like in an iframe.

Here's a quick fiddle which shows this... http://jsfiddle.net/Zv4zU/

But the javascript within the iframe never runs even though if you inspect the iframe the javascript is definitely in there. How can I get the javascript to run by itself?

like image 985
Timm Avatar asked Aug 08 '26 00:08

Timm


1 Answers

See this jsfiddle for a solution!

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

var html = "html code <script type='text/javascript'>alert('OK');<\/script>";
$("#myFrame").contents().find("body").html(html);
like image 182
Challe Avatar answered Aug 10 '26 13:08

Challe