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

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

问题描述

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



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



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

 < html> 
< head>
< script src =page1.jstype =text / javascript>< / script>
< / head>
< body>
...

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



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



 < html> 
< head>
< script type =text / javascript>
函数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);
}

函数unloadJSFile(){
delete window.foo;
删除window.cleanup;
alert(cleanup。typeof window.foo is+(typeof window.foo));
}
< / script>
< / head>
< body>

您好JavaScript删除

< br />
< button onclick =loadJSFile();>点击加载JS< / button>
< br />
< button onclick =foo();> call foo()< / button>
< br />
< button onclick =unloadJSFile();>点击卸载JS< / button>

< / body>
< / html>

simple.js来源:

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


解决方案

这不能完成。 / p>

执行脚本时,函数定义将添加到全局窗口对象中。可能有函数附加的调试符号,指示函数来自哪里,但是这些信息不可用于脚本。



关于您可以实现类似的唯一方法这将是在脚本中创建一个伪命名空间,然后在完成它时丢弃整个命名空间。然而,这个问题提示我,你正在尝试做错事。也许详细说明您的情况将有助于我们提供替代解决方案。


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

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.

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>
...

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 source:

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

解决方案

This cannot be done.

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天全站免登陆