[英]How to disable inline javascript in html?
我將用戶輸入的 html 放入 iframe 中。 目的是抑制 html 中的所有內聯 javascript。 曾想過使用 Regexp 刪除所有腳本,但它只捕獲了<script> ... </script>
,但沒有捕獲<txt onload='..javascript...
這是正則表達式: /<\\s*script[^>]*>[^<]*<\\s*\\/\\s*script\\s*>/gi
如何阻止輸入的 html 中的所有 js?
只需在 iframe 標記中設置sandbox=""
並且不允許腳本執行。
要弄清楚,您可以在此演示https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe 中添加sandbox
屬性,然后運行它並打開您的控制台。
Chrome 顯示此消息:
阻止了 '' 中的腳本執行,因為文檔的框架已被沙盒化並且未設置 'allow-scripts' 權限。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.