Daz wrote:
Hi everyone.
Is it possible to remove a script that has been added to a page?
Yes, but that simplistic answer is unlikely to be useful. I think what
you really want to know is how to release the memory allocated to
functions declared within a script.
I have
asked this question before, and was told that it was pretty much
impossible to do, unless of course, I deleted the function. I have had
a little think, and I might have stumbled upon a method of achieving
this, without having to know what functions you actually want to
delete. It might be a method already in practise, so I doubt it's
original, which is why I am asking as I am hoping someone can save me a
lot of time experimenting.
A simple experiment would take perhaps 10 minutes, exhaustive testing
may never be complete - such is life when scripting browsers.
If I dynamically load a script into a hidden iframe, and then delete
the iframe. Would that delete the script that are supposedly in it's
scope? Or would they remain, but be unusable.
Removing the iFrame and all references to it (including any closures
made to objects within its scope) will make it available for garbage
collection. When, or perhaps if, garbage collection runs, the memory
should be released. Testing will reveal whether or not it is in all
cases.
I would like to use
iframes almost like a namespace, for temporary scripts. Periodically, I
would like to remove the iframe, and replace it with a clean one. Can
anyone tell me off the top of their head if this is likely to work?
The iFrame trick is already used by some for cross-browser consistency
with dynamically loaded scripts, so I imagine it will "work" as you
describe. But why not just add the methods within an object namespace
as been explained previously? Then you can remove an entire namespace
by setting the object (or individual properties) to null.
Surely that is (much) simpler. I can't see that the iFrame trick has
any benefits over the object namespace method.
--
Rob