Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to stop a dynamically inserted script tag?

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();
like image 645
Martin Wittemann Avatar asked Jan 20 '10 10:01

Martin Wittemann


People also ask

How do you end a script tag?

Also to close an any element tag you must put the slash in front of the elements name. </script>, </html> etc.

How do I block third party scripts?

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.

Are script tags blocking?

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.

Do I need to close script tag?

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 /> .


1 Answers

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.

like image 85
Andy E Avatar answered Sep 21 '22 10:09

Andy E