admin管理员组文章数量:1391991
I hope you have well with nice pleasure. I am trying to remove Script tag by ID. For this purpose, I am apply code
var elem = document.getElementById("sns_scripts");
elem.remove();
But, this is give me error on my console windows is :
Uncaught TypeError: Cannot read property 'remove' of null
I am trying another method is :
$("#sns_scripts").remove();
It is also not working. See Picture
Picture is mention , sns_scripts script still avaialbe, and above this script my custom code, but it is not working. Please help me, I am really want to remove sns_scripts script from my webpage.
I hope you have well with nice pleasure. I am trying to remove Script tag by ID. For this purpose, I am apply code
var elem = document.getElementById("sns_scripts");
elem.remove();
But, this is give me error on my console windows is :
Uncaught TypeError: Cannot read property 'remove' of null
I am trying another method is :
$("#sns_scripts").remove();
It is also not working. See Picture
Picture is mention , sns_scripts script still avaialbe, and above this script my custom code, but it is not working. Please help me, I am really want to remove sns_scripts script from my webpage.
Share Improve this question asked Jun 20, 2016 at 11:35 ZohaibZohaib 3063 silver badges13 bronze badges 4-
Do you have iframes? give us more html around your
script
– Andrei Zhytkevich Commented Jun 20, 2016 at 11:39 - 2 This sounds like an XY problem. Why are you trying to remove in the first place? Are you aware that the script will already have been piled? – charlietfl Commented Jun 20, 2016 at 11:41
-
Just looks like what you want is to remove
canplay
event listener. Because actually, your question doesn't really make sense – A. Wolff Commented Jun 20, 2016 at 11:42 - Dear all members, website address is biznabler. – Zohaib Commented Jun 20, 2016 at 11:51
4 Answers
Reset to default 6Wrap your code within DOMContentLoaded
event:
document.addEventListener('DOMContentLoaded', function(){
var elem = document.getElementById("sns_scripts");
elem.remove();
});
Because you are trying to get an element at that point where is wasn't available in the DOM. So, it was null.
do not use elem.remove()
, but use
$(elem).remove();
NOTE: Re-order following lines in your code. Because you are running remove() function before writing script with id(sns_script):
<script type="text/javascript" id="sns_scripts">var video=document.getElementById('bgvid');</script>
<script>
$('#sns_scripts').remove();
</script>
Let's say you have a script in your DOM, with custom attribute: data-scriptid="MyDomScript"
:
<script src="jquery-3.6.0.min.js" data-scriptid="MyDomScript"></script>
Then you can remove it programmatically like this:
let domScripts = document.querySelectorAll('[data-scriptid="MyDomScript"]');
domScripts.forEach(function(s) {
s.remove();
});
Note that this will only remove the script tag from DOM, but not pletely erase the variables or events already defined by the script. You have to clear them manually.
本文标签: javascriptRemove Script TagStack Overflow
版权声明:本文标题:javascript - Remove Script Tag - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744685951a2619698.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论