需要检测网页中哪个<脚本>块与哪些其他<脚本>块交互

Need to detect which <script> block interact with which other <script> block in a webpage?

本文关键字:脚本 交互 其他 检测 网页      更新时间:2023-09-26

>我需要检查一个块中的javascript是否可以访问或操作网页中任何其他脚本块中的javascript。例如,第二个块(div 内部)访问正文中的第一个脚本块。

<body>
<script>
    var first_script_block=0;
</script>
<div>
<script >
    var secondblock_acess_first =first_script_block; 
</script>
</div>
</body>

我虽然很多。 我觉得很可怕。我需要一些想法。:(

所有脚本共享相同的global-object

如果你不希望它发生(很难从你的问题中分辨出来),请使用闭包:

<script>
(function (){
    // here is the code for the first script tag.
})();
</script>
...
<script>
(function (){
    // here is the code for the second script tag
})();
</script>

是的。查看此演示。看,您可以在一个变量中创建变量并在另一个变量中修改它。你的JS如何拆分为脚本块并不重要。

输出:

In first block
0
In second block
1