Why is it that scripts can still function even after the code used to create them is removed from the DOM?
I ran into a situation where I wanted to prevent a broken script from running (@see my post).
In my attempt to come up with a solution I wrote an extension with the following line (just to see what would happen).
$('script', doc).remove();
/*doc is passed here because this script is running as a firefox extension
outside of the document context.*/
I assumed that this would remove all the scripts from the DOM, which it did, and that therefore no scripts would run on the page, which is not the case.
I would love to know more about what’s behind this behavior.
The script is part of the DOM, sure, but the result of that script executing is left up to the javascript engine. Removing a script’s source (the part that’s in the DOM) will not remove existing varibles in the engine’s memory.