gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
Write a code for a html code runner
- User
Write a code for a html code runner
- Assistant
Here's an example code for an HTML code runner using JavaScript:
HTML:
<!DOCTYPE html> <html> <head> <title>HTML Code Runner</title> </head> <body> <textarea id="code" rows="8" cols="50" placeholder="Enter HTML code..."></textarea><br> <button onclick="runCode()">Run Code</button> <h3>Output:</h3> <iframe id="output" width="500" height="300"></iframe> <script src="runner.js"></script> </body> </html>JavaScript (runner.js):
function runCode() { var code = document.getElementById("code").value; var output = document.getElementById("output").contentWindow.document; try { output.open(); output.writeln(code); output.close(); } catch (error) { console.error(error); } }This code creates a textarea where the user can enter HTML code. When the "Run Code" button is clicked, the code is fetched using
document.getElementById("code").valueand then written into an iframe's document using thewritelnmethod.Note: Cross-Origin Resource Sharing (CORS) may affect the ability to load external resources (such as images or stylesheets) into the iframe in some cases.