删除 HTML 脚本标签对其包含的 JavaScript 有任何影响吗?

Does removing an HTML script tag have any affect on the JavaScript that it contains?

从我的测试来看,脚本标签似乎可以从 DOM 中删除,而不会对它包含的 JavaScript 产生任何影响。
此测试在执行 的过程中部分破坏了脚本 DOM 节点 。即使这对脚本没有影响,在 脚本标记已从 DOM 中删除后,变量 count 被赋予值 1

<!DOCTYPE html>
<html lang="en">
<head>
<title> Test </title>

<script id="jQueryScriptTag" src="https://code.jquery.com/jquery-1.11.2.min.js"></script>

</head>
<body>

<button id="testBtn">Click to test</button>

<div id="output"></div>

<script id="testCodeScriptTag">
    var count;

    $("#jQueryScriptTag").remove();
    $("#testCodeScriptTag").remove();
    $("#output").append(
        "<p>jQueryScriptTag is " + document.getElementById("jQueryScriptTag") + "</p>" +
        "<p>testCodeScriptTag is " + document.getElementById("testCodeScriptTag") + "</p>" +
        "<p>count is " + count + "</p>"
    );

    count = 1;

    $("#testBtn").click(function(){
        $("#output").append(
            "<p>count is " + (count++) + "</p>"
        );
    });

</script>

</body>
</html>

用例是从主机站点安全地删除注入的第三方脚本元素。

在执行 #testCodeScriptTag 的内容时,它们已经从 DOM 中获取,加载到 JavaScript 引擎中并进行了解析。 DOM 中的文本不再以任何方式连接到执行代码,as required by the HTML 5 specification:

1. Initialize the script block's source as follows:
...
If the script is inline and the script block's type is a text-based language
The value of the text IDL attribute at the time the element's "already started" flag was last set is the script source.
...
4. Create a script, using the script block's source, the URL from which the script was obtained, the script block's type as the scripting language, and the script settings object of the script element's Document's Window object.
...
Note: This is where the script is compiled and actually executed.

另请参阅:http://www.w3.org/TR/html5/webappapis.html#creating-scripts