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?
Scripts are blocking, so any script tag that appears before the closing body tag is downloaded, parsed and executed before the
DOMContentLoadedevent.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
DOMContentLoadedlisteners 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?