Is it somehow possible to stop a script tag from loading after is has been added to the head of a HTML document?
I would like to have something like this:
var script_tag = document.createElement('script');
script_tag.setAttribute('type', 'text/javascript');
script_tag.setAttribute('src', 'http://fail.org/nonexistant.js');
document.getElementsByTagName('head')[0].appendChild(script_tag);
// something like this!!!
script_tag.abort();
Also to close an any element tag you must put the slash in front of the elements name. </script>, </html> etc.
If you are using Google Tag Manager to include scripts on your website, you should use Google Tag Manager to block third-party scripts. Automatic script blocking will not work with Google Tag Manager since Google Tag Manager itself is a system to manage scripts without editing website code.
Under normal circumstances, a script tag causes the browser to halt rendering, load a file, and run the code. The browser is blocked from doing other useful work because your JavaScript could write to the page, modify existing elements, or redirect to another URL.
That's because SCRIPT TAG is not a VOID ELEMENT. In an HTML Document - VOID ELEMENTS do not need a "closing tag" at all! In xhtml, everything is Generic, therefore they all need termination e.g. a "closing tag"; Including br, a simple line-break, as <br></br> or its shorthand <br /> .
It's not possible in any cross-browser manner. As soon as the script tag is added to the head it will be downloaded and parsed and even removing the node won't stop it.
That being said, Firefox has document.onbeforescriptexecute
, which is cancellable (where the default action would be to execute the target script). This event was originally on a standards track but was recently removed from the HTML spec due to a lack of valid use cases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With