Javascript 通过 onclick 事件删除 <head> 标签中的特定 <script> 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14708189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove specific <script> tag in <head> tag by onclick event
提问by Aviway
<head>
<script type="text/javascript">
function include(filename, status){
if(status == 'on'){
var head = document.getElementsByTagName('head')[0];
script = document.createElement('script');
script.src = filename;
script.type = "text/javascript";
head.appendChild(script);
} else {
// The code that wipes the script tag above
}
}
</script>
</head>
<body>
<input type="button" value="OPEN" onclick="include('script.js', 'on')">
<input type="button" value="CLOSE" onclick="include('', 'off')">
</body>
I want to remove the specific tag in tag by onclick event. What code should be written in the ELSE area, When I click the "CLOSE" botton?
我想通过 onclick 事件删除标签中的特定标签。当我点击“CLOSE”按钮时,ELSE区域应该写什么代码?
回答by Sirko
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:
最简单的方法是以某种方式维护到创建元素的链接。例如,您可以将 include 函数放入一个闭包中并拥有一个私有变量来保存引用:
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!
一个重要的注意事项:通过删除<script>标签,您不会从 DOM 中删除它的任何对象、函数等。因此,在该<script>标签内开始的任何操作都将占上风,即使您删除了最初启动它的元素!
回答by Mx.
You could also add an ID to the ScriptElement
您还可以向 ScriptElement 添加一个 ID
this will work for you
这对你有用
function include(filename, status){
if(status == "on"){
var head = document.getElementsByTagName('head')[0];
script = document.createElement('script');
script.src = filename;
script.type = "text/javascript";
script.id = "testScriptName";
head.appendChild(script);
}else{
(elem=document.getElementById("testScriptName")).parentNode.removeChild(elem)
}
}
回答by sohel khalifa
Try using this:
尝试使用这个:
function include(filename, status){
var head = document.getElementsByTagName('head')[0];
if(status == on){
script = document.createElement('script');
script.src = filename;
script.type = text/javascript;
head.appendChild(script)
}
else if(status == 'off'){
var scripts = head.getElementsByTagName('script');
if(scripts.length > 0){
head.removeChild(scripts[0]);
}
}
}

