3

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?

2 Answers 2

1

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); 
Sign up to request clarification or add additional context in comments.

3 Comments

Please use fiddles as a supplement to your answer here. Please show your solution here, and link to a fiddle for demonstration purposes. This way, if jsfiddle were to even die, we'd still have the solution here.
Thanks, but that seems to be running the javascript on the parent, not on the iframe - example here: jsfiddle.net/BNG4n (the html of the page's 'test' div is shown, not the iframe's 'test' div, are there any fixes for this?
I'm going to ask above comment as new question as you did answer my initial question :)
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.