我将用户输入的 html 放入 iframe 中。目的是抑制 html 中的所有内联 javascript。曾想过使用正则表达式来删除所有脚本,但它只捕获了 <script> ... </script>
,但没有<txt onload='..javascript...
这是正则表达式:/<\s*script[^>]*>[^<]*<\s*\/\s*script\s*>/gi
如何屏蔽输入的html中的所有js?
最佳答案
只需在 iframe 标记中设置 sandbox=""
,就不会允许脚本执行。
要弄清楚这一点,您可以在此演示中添加 sandbox
属性 https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe ,然后运行它并打开控制台。
Chrome 显示此消息:
Blocked script execution in '' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.
关于javascript - 如何禁用 html 中的内联 JavaScript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60899915/