当jQuery .remove()用于删除脚本标记时,它是否清除了加载的javascript? [英] Does jQuery .remove() clear out loaded javascript when it is used to remove a script tag?

查看:152
本文介绍了当jQuery .remove()用于删除脚本标记时,它是否清除了加载的javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,如果我使用以下方法从DOM中删除脚本标记:

As the title says, if I remove a script tag from the DOM using:

$('#scriptid').remove();

javascript本身是保留在内存中还是已清除?

Does the javascript itself remain in memory or is it cleaned?

或者......我完全误解了浏览器对待javascript的方式吗?这很可能。

Or... am I completely misunderstanding the way in which browsers treat javascript? Which is quite possible.

对于那些对我的询问感兴趣的人,请看下面的内容:

For those interested in my reason for asking see below:

我正在移动一些从静态脚本文件到PHP中动态生成的常见javascript交互。当用户需要时按需加载哪些。

I am moving some common javascript interactions from static script files into dynamically generated ones in PHP. Which are loaded on demand when a user requires them.

执行此操作的原因是为了移动逻辑服务器端并运行从服务器返回的小脚本, 客户端。不是拥有包含大量逻辑的大型脚本,而是客户端。

The reason for doing this is in order to move the logic serverside and and run a small script, returned from the server, clientside. Rather than have a large script which contains a huge amount of logic, clientside.

这与facebook的做法类似......

This is a similar approach to what facebook does...

Facebook谈论前端javascript

如果我们举一个简单的对话框。而不是在javascript中生成html,将其附加到dom,然后使用jqueryUI的对话框小部件加载它,我现在正在执行以下操作。

If we take a simple dialog for instance. Rather than generating the html in javascript, appending it to the dom, then using jqueryUI's dialog widget to load it, I am now doing the following.


  • 对dialog.php发出Ajax请求

  • 服务器生成特定于此对话框的html和javascript,然后将它们编码为JSON

  • JSON是返回客户端。

  • HTML附加到< body> 然后一旦渲染,javascript也会附加到DOM。

  • Ajax request is made to dialog.php
  • Server generates html and javascript that is specific to this dialog then encodes them as JSON
  • JSON is returned to client.
  • HTML is appended to the <body> then once this is rendered, the javascript is also appended into the DOM.

插入后会自动执行javascript并打开动态对话框。

The javascript is executed automatically upon insertion and the dynamic dialog opens up.

这样做大大减少了我页面上javasript的数量,但我担心清理插入的javascript。

Doing this has reduced the amount of javasript on my page dramatically however I am concerned about clean up of the inserted javascript.

显然,一旦对话框关闭它使用jQuery从DOM中删除:

Obviously once the dialog has been closed it is removed from the DOM using jQuery:

$('#dialog')。remove();

javascript附加了ID和I.也可以通过相同的方法从DOM中删除它。

The javascript is appended with an ID and I also remove this from the DOM via the same method.

但是,如上所述,使用jQuery的.remove()实际上是从内存中清除javascript还是简单从DOM中删除< script> 元素?

However, as stated above, does using jQuery's .remove() actually clean out the javascript from memory or does it simple remove the <script> element from the DOM?

如果是这样,有没有办法清理它?

If so, is there any way to clean this up?

推荐答案

没有。加载脚本后,它定义的对象和函数将保留在内存中。删除脚本元素不会删除它定义的对象。这与CSS文件形成对比,其中删除元素会删除它定义的样式。那是因为新款式很容易回流。你能想象得出脚本标签创建的内容以及如何删除它有多难吗?

No. Once a script is loaded, the objects and functions it defines are kept in memory. Removing a script element does not remove the objects it defines. This is in contrast to CSS files, where removing the element does remove the styles it defines. That's because the new styles can easily be reflowed. Can you imagine how hard it would be to work out what a script tag created and how to remove it?

编辑:但是,如果你有一个定义<$的文件c $ c> myFunction ,然后添加另一个脚本,将 myFunction 重新定义为其他内容,将保留新值。如果你想保持DOM干净,你可以删除旧的脚本标签,但这一切都是删除它。

However, if you have a file that defines myFunction, then you add another script that redefines myFunction to something else, the new value will be kept. You can remove the old script tag if you want to keep the DOM clean, but that's all removing it does.

EDIT2:我能想到的清理功能的唯一真正方法是为每个可能的对象和函数提供一个基本上调用删除window.myFunction 的JS文件您的其他脚本文件可能会定义。出于显而易见的原因,这是一个非常糟糕的主意。

The only real way to "clean up" functions that I can think of is to have a JS file that basically calls delete window.myFunction for every possible object and function your other script files may define. For obvious reasons, this is a really bad idea.

这篇关于当jQuery .remove()用于删除脚本标记时,它是否清除了加载的javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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