简体   繁体   English

使用 JS 从 html 和 DOM 中删除脚本标签

[英]Remove script tag from html and from the DOM with JS

How to remove a js script from the html and from the DOM?如何从 html 和 DOM 中删除 js 脚本?

To exemplify, in my html i have a div with a button:例如,在我的 html 中,我有一个带按钮的 div:

<div class="tester">

    <button id="myBtn">Click me!</button>

</div>

And it has also this script <script src="js/debug.js" id="debug_js"></script>它还有这个脚本<script src="js/debug.js" id="debug_js"></script>

The code of debug.js : debug.js的代码:

window.addEventListener("DOMContentLoaded", initApp1);


function initApp1()
{

    console.log("Init App!! ");

    var myBtn = document.getElementById("myBtn");
    var script = document.getElementById("debug_js");

    myBtn.addEventListener("click", function(){

        alert("clicked!");

        $( script ).remove();

    });

} //    initApp1

When i do the first click, it removes the script from the html.当我第一次点击时,它会从 html 中删除脚本。 Ok.好的。 When i click again i expect this behaviour: DOn't make the alert "clicked!"当我再次点击时,我希望有这种行为:不要让警报“点击!” (actually Do nothing). (实际上什么都不做)。

The behaviour that i'm having: It throws the alert "clicked!"我的行为:它抛出警报“点击!” again (every time the btn is clicked)再次(每次点击 btn 时)

After some search i found that: " By removing the tag, you do not remove any of its objects, functions etc. from the DOM. So any action started within that tag will prevail, even if you delete the element, that started it in the first place! "经过一番搜索,我发现:“通过删除标签,您不会从 DOM 中删除它的任何对象、函数等。因此,在该标签内启动的任何操作都将占上风,即使您删除了启动它的元素第一名!

So how can i remove it also from the DOM ?那么我怎样才能从 DOM 中删除它呢? (or other suggestions) (或其他建议)

You can check for the script in the button click event您可以在按钮单击事件中检查脚本

var myBtn = document.getElementById("myBtn");

myBtn.addEventListener("click", function(){
    var script = document.getElementById("debug_js");
    if(script){
        alert("clicked!");

        $( script ).remove();
    }
}); 

Or you can remove the event handler after clicking或者您可以在单击后删除事件处理程序

var myBtn = document.getElementById("myBtn");
var script = document.getElementById("debug_js");

var btnHandler = function(){
    alert("clicked!");
    $( script ).remove();
    myBtn.removeEventListener("click", btnHandler);
}

myBtn.addEventListener("click", btnHandler);

There are two general approaches to remove all script tags from HTML with Javascript :使用JavascriptHTML删除所有script标签有两种通用方法:

REGEX正则表达式

const patt = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script\s*>/gi
const html = document.getElementsByTagName('html')[0]
html.innerHTML =  html.innerHTML.replace(patt, '')

Insert it as the innerHTML of a div , see here将其作为 div 的 innerHTML 插入,请参见此处

But... any script which got evaluated once by the engine will stay in memory for the rest of your session.但是......引擎评估过的任何脚本都将在您的会话剩余时间内保留在内存中。 Even by removing the entire script node where the code was contained doesn't change that fact.即使删除包含代码的整个脚本节点也不会改变这一事实。

To get rid of the script effect you can choose at least two strategies:要摆脱脚本效果,您至少可以选择两种策略:

  • document.write(html.innerHTML); after cleaning html.innerHTML from scripts (see above)从脚本中清除 html.innerHTML 后(见上文)
  • clean eventListeners by EventTarget.removeEventListener()通过EventTarget.removeEventListener()清理 eventListeners

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM