简体   繁体   English

如何动态卸载 javascript 文件?

[英]How can I dynamically unload a javascript file?

I am including pages using Ajax but I also need to include their respective javascript files, which requires removing the previous javascript files from memory at the same time.我包括使用 Ajax 的页面,但我还需要包括它们各自的 javascript 文件,这需要同时从内存中删除以前的 javascript 文件。

How can I unload the currently-loaded javascript files (as well as their code in memory) so that I can load the new page's files?如何卸载当前加载的 javascript 文件(以及它们在内存中的代码),以便加载新页面的文件? They will more than likely conflict, so having multiple independent files' javascript files loaded.它们很可能会发生冲突,因此加载了多个独立文件的 javascript 文件。

This really sounds like you need to reevaluate your design. 听起来确实像您需要重新评估设计。 Either you need to drop ajax, or you need to not have collisions in you method names. 您需要删除Ajax,或者方法名称中不需要冲突。

You can review this link: http://www.javascriptkit.com/javatutors/loadjavascriptcss2.shtml 您可以查看以下链接: http : //www.javascriptkit.com/javatutors/loadjavascriptcss2.shtml

Which gives information on how to remove the javascript from the DOM. 其中提供了有关如何从DOM中删除JavaScript的信息。 However, modern browsers will leave the code in memory on the browser. 但是,现代浏览器会将代码保留在浏览器的内存中。

No, you can't do that. 不,你不能那样做。 Once a block of JavaScript gets loaded in the browser and executed, it gets stored in browser memory under the scope of the respective window. 一旦将JavaScript块加载到浏览器中并执行后,它就会存储在相应窗口范围内的浏览器内存中。 There is absolutely no way to unload it (without page refresh/window close). 绝对没有办法卸载它(没有页面刷新/窗口关闭)。

you can just namespace your code.. that way you prevent collisions 您可以只为代码命名空间..这样可以防止冲突

var MyJavaScriptCode = {}; var MyJavaScriptCode = {};

MyJavaScriptCode.bla = function () {}; MyJavaScriptCode.bla = function(){};

Actually that's quite possible. 其实那是完全可能的。 You can replace an script or link element. 您可以替换脚本或链接元素。

function createjscssfile(filename, filetype){
if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
}

else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
}
  return fileref
}

function replacejscssfile(oldfilename, newfilename, filetype){
var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist using
var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
var allsuspects=document.getElementsByTagName(targetelement)
for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
    if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(oldfilename)!=-1){
        var newelement=createjscssfile(newfilename, filetype)
        allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i])
    }
  }
}

you must fill filename parameters as src attribute and filetype as "js" or "css" 您必须将文件名参数填充为src属性,并将文件类型填充为“ js”或“ css”

I think there's no need to explain the code. 我认为没有必要解释代码。 Also you've posted in 2009 but hey. 您还发布了2009年,但嘿。 Maybe someone will need it right? 也许有人会需要它吗? :) :)

All credit goes to: http://www.javascriptkit.com/javatutors/loadjavascriptcss2.shtml 所有功劳归功于: http : //www.javascriptkit.com/javatutors/loadjavascriptcss2.shtml

You can learn some tricks there btw. 您可以在那里学习一些技巧。

It is possible to unload a javaScript file clearing the entire code from the browser. 可以卸载JavaScript文件,从浏览器中清除整个代码。 The steps are: 这些步骤是:

  1. Give an ID to the target " script " element (eg " mainJs ", pointing to " main.js ") 给目标“ 脚本 ”元素一个ID(例如,“ mainJs ”,指向“ main.js ”)
  2. Create another JS file (eg " flush.js "), with the same functions and variables in " main.js ", and make sure the functions and variables contents are empty. 创建另一个JS文件(例如“ flush.js ”),在“ main.js ”中使用相同的函数和变量,并确保函数和变量的内容为空。
  3. After " main.js " has been used up, we flush it by deleting the " script#mainJs " element and loading the " flush.js " dynamically into the DOM tree. 在“ main.js ”用完之后,我们通过删除“ script#mainJs ”元素并将“ flush.js ”动态加载到DOM树中来刷新它。

Example

mainJs    = document.getElementById("mainJs");
document.head.removeChild(mainJS);//Remooves main.js from the DOM tree

flushFile = document.createElement("script");
flushFile.setAttribute("src", "flush.js");
document.head.appendChild(flushFile);//Loads flush.js into the DOM tree, overwriting all functions and variables of main.js in browser memory to null

That's it 而已

Exactly the similar requirement I've and I've tried in following way but it requires a small coding part from developer side even.正是我和我尝试过的类似要求,但它甚至需要来自开发人员的一小部分编码部分。

I've a js file with 20+ functions as below and file name is profile.js我有一个包含 20 多个函数的 js 文件,如下所示,文件名为 profile.js

function myprofile(){
//code block A
}  
function updateprofile(){
//code block B
}....
var functionsInJs=["myprofile","updateprofile",...]

Now I've written a method to clear the memory of this file as follows现在我写了一个方法来清除这个文件的内存如下

function unloadJs(array){
    array.forEach(function(eachJsFn){
         window[eachJsFn]=undefined;//Clearing the functionality of js function in browsers window
    }
}

So, while to unload js functions i'll pass the array of function names in it.因此,在卸载 js 函数时,我将在其中传递函数名称数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM