Is it possible to block the downloading of external scripts/stylesheets with a bit of javascript? For example, putting a tiny block of javascript at the very top of <head> to do some operation on all <script> and <link> tags in the document, perhaps changing relative paths to absolute paths, before their original relative-path’d sources are downloaded from the server?
I am not looking for alternatives such as server-side processing or manually altering the <script> and <link> tags. I am interested in a proof-of-concept for the scenario where i have no control over the final document or it’s generation except for inserting a single script tag.
No, you cannot.
The problem is in the head of the document the script will not have access to the nodes that are not yet loaded, so it cannot modify any script tags that will be coming up.
Also, the browser loads JavaScript as soon as it gets to the script tag and halt parsing the html until the script tag completes.
That means, there is no way of doing it with JavaScript.