The easiest way, would be to somehow maintain a link to the created element. For example, you could put the include function into a closure and have a private variable, to hold the reference:
var include = (function(){
// the reference to the script
var theScript;
return function (filename, status){
if(status == 'on'){
// adding a script tag
var head = document.getElementsByTagName('head')[0];
theScript= document.createElement('script');
theScript.src = filename;
theScript.type = "text/javascript";
head.appendChild( theScript )
}else{
// removing it again
theScript.parentNode.removeChild( theScript );
}
}
})();
One important note: By removing the <script>
tag, you do not remove any of its objects, functions etc. from the DOM. So any action started within that <script>
tag will prevail, even if you delete the element, that started it in the first place!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…