JavaScript-从HTML删除脚本标签

时间:2018-08-14 16:44:56

标签: javascript html

加载HTML文件时,我想运行一个脚本,该脚本将基于用户代理从HTML文件中删除一些脚本标签。

为此,我遵循了以下问题:Removing all script tags from html with JS Regular Expression

但是无法实现我所需要的,脚本标记已删除,并且正在下载。

HTML:

5.81

在Chrome中运行<html> <head> </head> <body> <p></p> <script type="text/javascript"> function stripScripts(s) { var div = document.createElement('div'); div.innerHTML = s; var scripts = div.getElementsByTagName('script'); var i = scripts.length; while (i--) { scripts[i].parentNode.removeChild(scripts[i]); } return div.innerHTML; } var userAgent = navigator.userAgent; console.log(userAgent); if (!userAgent.includes("Chrome")) { stripScripts(); // Here how can I call the present index.html? } else { var para = document.createElement("P"); var t = document.createTextNode("You cannot view this in Chrome"); para.appendChild(t); document.body.appendChild(para); }; </script> <script type="text/javascript" src="scripts2.js"></script> <!-- load only when the condition is met else remove it --!> </body> </html> 时,index.html一直在加载。

0 个答案:

没有答案