首页 > 解决方案 > How to refresh Javascript code in IFrame?

问题描述

So, I am trying to experiment with making a code editor. I have an IFrame with a preview window that is linked to another HTML document in my webpage where the code would be executed. (I need to execute code with the p5 library) This is the code when I click on "run code" button -

try {
    var script = document.createElement( 'script' );
    script.id = "code"
    script.text  = code
    $('#preview').contents().find('#codeplaceholder').html(script)

    $('#preview').contents().find('#code')
    document.getElementById('preview').contentWindow.location.reload();
  } catch (e) {
    alert(e);

and here is the tags in my HTML preview document:

<!DOCTYPE html>
<html>
<head>
<script id = "p5" 
src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.min.js"></script>

</head>

<body>
<div id="codeplaceholder">

</div>
</body>

</html>

so what's wrong? Then I try to run code with the p5 library, nothing happens. I am out of ideas. Seems like "Alert" messages work, but p5 is just really stubborn...

标签: javascripthtmlexecutep5.js

解决方案


推荐阅读