Reputation: 16795
I've been trying to get this code working in Google Chrome but I can't seem to be able to stop server requests from "removed" scripts from happening.
What I'm trying to do is remove all scripts in a page, the code below removes the scripts from the DOM but they still load and execute.
Code:
document.addEventListener("DOMContentLoaded", function(){
var e2 = document.getElementsByTagName("script");
for(var i = e2.length; i--; e2[i].parentNode.removeChild(e2[i]));
});
Any ideas?
Upvotes: 2
Views: 2522
Reputation: 55613
Scripts are blocking, so any script tag that appears before the closing body tag is downloaded, parsed and executed before the DOMContentLoaded
event.
You could always take a look at async or defer attributes on your script tags, but you'd still be guessing if they have been executed or not by the time your DOMContentLoaded
listeners gets executed.
You should really try to look for an alternative method to achieve what you want to accomplish here. Might I ask what you're trying to do?
Upvotes: 4