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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744685951a4587915.html
评论列表(0条)