javaScript不是DOM的一部分吗? [英] Is the javaScript not part of the DOM?

查看:152
本文介绍了javaScript不是DOM的一部分吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么脚本仍然可以运行,即使从DOM中删除用于创建代码的代码?

一种情况,我想防止破坏的脚本运行( @看到我的帖子)。

I ran into a situation where I wanted to prevent a broken script from running (@see my post).

在我试图想出一个解决方案写了一个扩展名,使用以下行(只是看看会发生什么)

In my attempt to come up with a solution I wrote an extension with the following line (just to see what would happen).

$('script', doc).remove();
/*doc is passed here because this script is running as a firefox extension 
  outside of the document context.*/

我假设这将从DOM中删除所有的脚本,这样做就是这样,所以没有脚本会在页面上运行,而不是这样。

I assumed that this would remove all the scripts from the DOM, which it did, and that therefore no scripts would run on the page, which is not the case.

我很想知道这个行为背后的内容。

I would love to know more about what's behind this behavior.

推荐答案

脚本是一部分的DOM,但是执行该脚本的结果是JavaScript引擎。删除脚本的源代码(DOM中的部分)不会删除引擎内存中的现有变量。

The script is part of the DOM, sure, but the result of that script executing is left up to the javascript engine. Removing a script's source (the part that's in the DOM) will not remove existing varibles in the engine's memory.

这篇关于javaScript不是DOM的一部分吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆