Since inline module scripts don’t have document.currentScript
, we can’t simply append document.currentScript.remove()
at the end of the script.
Extra Notes/Context:
- I’m talking about the synchronous phase of the module script’s execution. I.e. once the script has synchronously executed, it should immediately be deleted, without e.g. waiting for an event loop tick in which other code might run.
- I’m adding the module script to the DOM programmatically, and I don’t control the code inside the script tag (i.e. it’s “userland” code), or whether or not it’s a module script, and so on. I can only do simple modifications to the script that don’t affect the code – e.g. appending
document.currentScript.remove()
would be fine, but e.g. wrapping the script in a function/block scope, or something like that would materially affect the code (e.g. variable scoping), so I can’t do stuff like that. - This is an unusual requirement, I must admit, but it’s an unusual use case – related to dev tooling / live debugging / etc.