在iframe中注入js

inject js inside iframe

本文关键字:js 注入 iframe      更新时间:2023-09-26

简单地说,我正在尝试将我自己的"自己尝试"页面实现为w3schools,它使我能够获取html和js代码并在iframe 中运行它

这是我的代码

<html><body>

<script>
    function update()
    {document.getElementById('display').contentDocument.documentElement.innerHTML = document.getElementById('mycode').value;
    }
</script>
<input onclick="update()" type="button" value="Update page" sandbox>
<textarea id="mycode"></textarea>
<iframe id="display" sandbox="allow-same-origin allow-scripts allow-popups allow-forms"></iframe></body></html>

但当我在文本区域内设置代码时,它不会运行任何脚本我尝试了沙箱属性,但没有结果

您基本上想要实现一个try-it-yourself-page。要做到这一点,你可以试试这个。

<html>
<body>
<script>
   function update{
      '#display'.load(document.getElementById('mycode').value);
   }
</script>
<input onclick="update()" type="button" value="Update page">
<textarea id="mycode"></textarea>
<iframe id="display"></iframe>
</body>
</html>