简体   繁体   English

你如何禁用<script> elements using JavaScript

[英]How do you disable <script> elements using JavaScript

I want to disable <script> tags.我想禁用<script>标签。 Here's my code, but it doesn't work.这是我的代码,但它不起作用。

document.getElementsByTagName('script').disabled = true;

In fact, it is possible to disable execution by changing "type" attribute:实际上,可以通过更改“type”属性来禁用执行:

<script type="text/javascript">
    alert("I will alert you");
</script>

<script type="application/json">
    alert("And I will keep silent");
</script>

<script>
    alert("I will alert too");
</script>

http://jsfiddle.net/r6c0x0sc/ http://jsfiddle.net/r6c0x0sc/

无法完成...脚本标签在 DOM 渲染器渲染后立即评估,因此在 wards 之后处理它不会做太多事情。

You can disable a script element.您可以禁用脚本元素。 There are a couple of ways:有几种方法:

You can specify a different script type as other have listed.您可以指定与其他脚本类型不同的脚本类型 Heres the one that worked for me:这是对我有用的一个:

//loop through all script tags on page
$('script').each(function(){
    var scripthtml = $(this).html();
    $(this).replaceWith('<script type="text/gzip">' + scripthtml + '</script>');
});

All of the official script types can all be found here: iana.org所有官方脚本类型都可以在这里找到: iana.org

The second way is just a simple if statement :第二种方法只是一个简单的 if 语句

//loop through all script tags on page
$('script').each(function(){
    var scripthtml = $(this).html();
    $(this).replaceWith('<script>if (1==0){' + scripthtml + '}</script>');
});

The if statement will always be false, so anything inside the script tag won't execute. if 语句将始终为 false,因此脚本标记内的任何内容都不会执行。 However all of your functions() inside the script tag will all be valid.但是,脚本标记内的所有函数() 都将有效。

Heres the javascript equivalent of replaceWith:继承人的javascript 相当于replaceWith:

//get script and html
var yourscripttag = document.getElementById('yourscripttagID');
var scripthtml = 'if (1==0){' + yourscripttag.innerHTML + '}';
//remove script
yourscripttag.remove();
//create new script element
var newscript=document.createElement('script');
newscript.type='text/javascript';
//insert html in new script tag
newscript.appendChild(document.createTextNode(scripthtml));
//insert new script tag into head
document.getElementsByTagName('head').item(0).appendChild(newscript);

As I understand you are getting some HTML code from the Database and add it to the page.据我了解,您正在从数据库中获取一些 HTML 代码并将其添加到页面中。 If so, You can write a Server Side function that will add a HTML Notes around the scripts using a simple Regex and after that the user saving the changes you will need to remove the notes.如果是这样,您可以编写一个服务器端函数,该函数将使用简单的正则表达式在脚本周围添加 HTML 注释,然后用户保存更改,您将需要删除注释。

You can do it also in Javascript if you have the HTML string that you like to add before you add it to the Dom.如果您在将 HTML 字符串添加到 Dom 之前想要添加它,您也可以在 Javascript 中执行此操作。

Removing all the events on DOM nodes will prevent most of the JavaScript from executing (here for document ):删除 DOM 节点上的所有事件将阻止大部分 JavaScript 执行(此处针对document ):

for (key in getEventListeners(document)) {
  getEventListeners(document)[key].forEach(function(c) {
    c.remove()
  })   
}

You can hack it.你可以破解它。 Use another script to do a....使用另一个脚本来做一个......

node.parentNode.replaceChild(
  document.createComment(node.outerHTML.slice(1,-1)
    .replace(/--/g, '\\-\\-')), node);

Where node is your script node/element.其中 node 是您的脚本节点/元素。 The replacing of double dashes is to prevent the browser complaining about an uncompliant comment tag.替换双破折号是为了防止浏览器抱怨评论标签不合规。

Then if you want to enable it just do the reverse comment.replace and enclose with < and >.然后,如果您想启用它,只需执行反向 comment.replace 并用 < 和 > 括起来。 But you probably need to keep track of the modified script node, because after it becomes a comment it is less query-able.但是您可能需要跟踪修改后的脚本节点,因为在它成为注释之后,它的查询能力就变差了。

抱歉,您无法禁用<script>元素。

Inline Java is a security risk.内联 Java 存在安全风险。 But this can be mitigated using a simple solution.但这可以通过一个简单的解决方案来缓解。

Simply replace any instance of with something like <script&rt;只需将 的任何实例替换为 <script&rt; 之类的东西; so it does not render as a tag, but will output in HTML appearing as if it was a tag.因此它不会呈现为标签,而是会以 HTML 格式输出,看起来就像是标签一样。

input.replace("<script>", "&lt;script&gt;")
     .replace("</script>", "&lt;&#47;script&gt;")

This will work if your use case is something like:如果您的用例类似于:

  1. If you want to disable inline script such as如果要禁用内联脚本,例如

    <script> alert("Hello"); <script> alert("你好"); </script> </脚本>

  2. If you don't want to remove the content(script tag data) from display.如果您不想从显示中删除内容(脚本标记数据)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用onclick javascript函数禁用按钮? - How do you disable a button using an onclick javascript function? 如何使用javascript和jquery重新排列元素中的文本 - How do you rearange text in elements using javascript and jquery 如何使用 JavaScript 在某个元素之后添加元素 - How do you add an element after a certain elements using JavaScript 如何在JL的ESLint中禁用分号? - How do you disable semicolons in ESLint for Javascript? 在使用 Javascript 传递一组 if 语句之前,如何禁用按钮? - How do you disable a button until a set of if statements are passed using Javascript? 使用JavaScript,如何使用getElementByClassName获取具有位于另一个元素内的特定className的元素? - Using JavaScript, how do you get elements with a specific className that's inside another element by using getElementByClassName? 如何使用脚本标记中的HTML使用JavaScript替换字符串 - How do you do a string replace with javascript using HTML in a script tag 你如何在python的机械化中禁用javascript? - How do you disable javascript in python's mechanize? 使用JavaScript验证时如何忽略隐藏的页面元素 - How do you ignore hidden page elements when validating using javascript 如何使用JavaScript禁用TextBox? - How do I disable TextBox using JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM