Okay, I am probably missing something totally obvious here, but I have been fiddling with this all day and can't seem to turn up a solution... and I'm getting tired!
I have a page where I want pupils to edit (HTML/JavaScript) code from a form 'textarea' and the submit to preview it in an iframe embedded on the same page, in the same way the W3C Schools website does.
Works great... until you try to do something simple like run a script from an event.
I am reasonably sure that it is something to do with the browser sandboxing it, does anyone have experience with this and how to work round it?
Simple example;
1) Pupil writes an 'hello world alert' function triggered by onClick from a form button (Click me);
2) Pupil clicks 'preview code';
3) iframe grabs the incoming POST variable (the students code) and displays the form with the 'Click me' button;
4) Pupil clicks the 'Click me' but the script fails to produce the expected alert (does nothing).
I have tried everything I can think of including trying the experimental HTML5 sandbox flags to allow forms and scripts, but no joy.
UPDATE: The code runs fine in IE and FF, but fails in Chrome, Opera and Safari.
'Parent' Code (Simplified):
[CODE]
<form action="result.php" method="post" target="codeView" id="codeForm">
<textarea id="codeInput" name="codeInput">
<?php include("lessoncode.php"); // pulls in and displays the sample lesson code ?>
</textarea>
<input name="submit" type="submit" value="Run your code">
</form>
[CODE]
iframe Code (Simplified):
[CODE]
<iframe frameborder="0" name="codeView" src="result.php"></iframe>
[CODE]
Result.php (Simplified):
[CODE]
<?php
if(isset($_POST["codeInput"])){
$outputcode = $_POST["codeInput"];
print_r ($outputcode);
}
else {echo "Your code will execute in this window";}
?>
[CODE]