在运行时加载和卸载javascript

在运行时加载和卸载javascript,javascript,Javascript,是否可以在运行时通过JavaScript添加或删除JavaScript文件和CSS文件?如果JavaScript无法实现,那么我们可以通过PHP或任何其他服务器站点语言实现吗 事实上,我尝试了yep nope js,但我无法通过它卸载任何javascript 是否可以在运行时通过JavaScript添加或删除JavaScript文件和CSS文件 您可以动态加载JavaScript,但是一旦加载了JavaScript,就无法在活动页面中卸载JavaScript。您可以重新加载一个页面,该页面将清除

是否可以在运行时通过JavaScript添加或删除JavaScript文件和CSS文件?如果JavaScript无法实现,那么我们可以通过PHP或任何其他服务器站点语言实现吗

事实上,我尝试了yep nope js,但我无法通过它卸载任何javascript

是否可以在运行时通过JavaScript添加或删除JavaScript文件和CSS文件

您可以动态加载JavaScript,但是一旦加载了JavaScript,就无法在活动页面中卸载JavaScript。您可以重新加载一个页面,该页面将清除活动脚本,然后重新开始。或者,您可以覆盖已设置的函数

就CSS而言,您可以添加/删除
元素来添加/删除样式表。此外,您还可以添加/删除可能包含规则的
元素

您还可以使用DHTML修改页面中元素的任何内联样式。

检查,然后

从顶部链接,动态添加js或css:

function loadjscssfile(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)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}

loadjscssfile("myscript.js", "js") //dynamically load and add this .js file
loadjscssfile("javascript.php", "js") //dynamically load "javascript.php" as a JavaScript file
loadjscssfile("mystyle.css", "css") ////dynamically load and add this .css file
function removejscssfile(filename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
 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(filename)!=-1)
   allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
 }
}    
removejscssfile("somescript.js", "js") //remove all occurences of "somescript.js" on page
removejscssfile("somestyle.css", "css") //remove all occurences "somestyle.css" on page
另外,要动态删除js或css:

function loadjscssfile(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)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}

loadjscssfile("myscript.js", "js") //dynamically load and add this .js file
loadjscssfile("javascript.php", "js") //dynamically load "javascript.php" as a JavaScript file
loadjscssfile("mystyle.css", "css") ////dynamically load and add this .css file
function removejscssfile(filename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
 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(filename)!=-1)
   allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
 }
}    
removejscssfile("somescript.js", "js") //remove all occurences of "somescript.js" on page
removejscssfile("somestyle.css", "css") //remove all occurences "somestyle.css" on page
但请注意删除:

那么,当您删除外部JavaScript或CSS文件时,实际会发生什么呢?也许不完全是你所期望的。在 如果是JavaScript,则从文档中删除元素 树中,作为外部JavaScript文件一部分加载的任何代码都会保留 在浏览器的内存中。也就是说,您仍然可以访问 外部文件第一次加载时添加的变量、函数等 装载


不,你不能那样做。一旦JavaScript块加载到浏览器中并执行,它就会存储在相应窗口范围内的浏览器内存中。绝对没有办法卸载它(如果没有页面刷新/窗口关闭)


最多,您所能做的就是将javascript文件中包含的变量设置为Null。但同样,这并不能完全消除问题,但是,是的,对这些变量的任何进一步访问都不会产生结果。

谢谢Mikey,实际上我用jQuery做了同样的操作,它工作得很好,但我在firebug中检查了它,它没有显示脚本标记,这就是我感到困惑的原因。但它的功能如预期的那样工作:)再次感谢