如何从 html 中卸载 javascript? [英] How to unload a javascript from an html?

查看:41
本文介绍了如何从 html 中卸载 javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 DOM 中卸载 JavaScript 资源及其所有定义的对象?

How can I unload a JavaScript resource with all its defined objects from the DOM?

我正在开发一个简单的框架,可以将 html 片段加载到主"html 中.每个片段都是自包含的,可能包含对其他 JS 和 CSS 文件的引用.JS 和 CSS 资源被解析并动态添加到 html.当片段从 DOM 中删除/替换时,我想删除它的 JS 和 CSS.

Im developing a simple framework that enables to load html fragments into a "main" html. Each fragment is self contained and may include references to additional JS and CSS files. The JS and CSS resources are parsed and dynamically added to the html. When the fragment is removed/replaced from the DOM I want to remove its JS and CSS.

如果我删除下面示例中的脚本元素,page1.js 中定义的函数仍然可用.

If I remove the script element in the example below, the functions defined in page1.js are still available.

<html>
  <head>
    <script src="page1.js" type="text/javascript"></script>
  </head>
<body>
...

有没有办法从 DOM 中卸载 page1.js 对象?

Is there a way to unload page1.js objects from the DOM?

========我使用的测试代码 ========

========= The test code I use =======

我尝试了我在下面评论中得到的建议;使用清理功能删除添加的对象 - 但即使这样也会失败.我用于测试的来源:

I tried the advice i got in the comments below; to delete the added objects using a cleanup function - but even this fails. The sources i used for testing:

<html>
<head>
<script type="text/javascript">
    function loadJSFile(){
        var scriptTag = document.createElement("script");
        scriptTag.setAttribute("type", "text/javascript");
        scriptTag.setAttribute("src", "simple.js");

        var head = document.getElementsByTagName("head")[0];
        head.appendChild(scriptTag);
    }

    function unloadJSFile(){            
        delete window.foo;
        delete window.cleanup;
        alert("cleanedup. typeof window.foo is " + (typeof window.foo));
    }
</script>
</head>
<body>

  Hello JavaScript Delete

  <br/>
  <button onclick="loadJSFile();">Click to load JS</button>
  <br/>
  <button onclick="foo();">call foo()</button>
  <br/>
  <button onclick="unloadJSFile();">Click to unload JS</button>

</body>
</html>

simple.js 源码:

simple.js source:

var foo = function(){
    alert("hello from foo");
}

推荐答案

这无法做到.

执行脚本时,函数定义会添加到全局window 对象中.可能有调试符号附加到函数上,表明函数来自哪里,但脚本无法使用此信息.

When a script is executed, function definitions are added to the global window object. There may be debugging symbols attached to the function that indicate where the function came from, but this information is not available to scripts.

关于实现这样的事情的唯一方法是在脚本中创建一个伪命名空间,然后在完成后丢弃整个命名空间.但是,这个问题向我暗示您正试图以错误的方式做某事.或许详细说明您的场景将有助于我们提供替代解决方案.

About the only way you could achieve something like this would be to create a pseudo-namespace in the script and then throw away that whole namespace when you are done with it. However, this question hints to me that you are trying to do something the wrong way. Perhaps elaborating on your scenario would help us provide alternate solutions.

这篇关于如何从 html 中卸载 javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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