Timm
Timm

Reputation: 12753

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?

Upvotes: 3

Views: 2851

Answers (2)

zool
zool

Reputation: 106

I'm bit update your script to really start JS in the iframe:

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

see jsfeedle

Upvotes: 0

Challe
Challe

Reputation: 896

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);

Upvotes: 1

Related Questions