On Wed, Aug 3, 2011 at 4:10 PM, Stefan Weiss <[email protected]> wrote:

> On 2011-08-03 15:49, UI Architect wrote:
> > Is there a way to unload an already loaded script? Let's say, I put a
> > <script> tag with src to an external JS file. It gets loaded and
> > executed in the browser. After this point, is there any way to unload
> > (also from browser memory) that script?
>
> Not reliably. If you know exactly what the script did, you can try to
> reverse its effects, but that's not always possible. For example, the
> included script could do something like -
>
>   (function () {
>      function butIRepeatMyself () {
>         alert("Am I annoying you?");
>      }
>      window.setInterval(butIRepeatMyself, 5000);
>   })();
>
> - and there's nothing you can do to stop it (short of navigating away).
>

Just pedantry (I agree with the general point), but you can remove a
setInterval if you can guess the numeric id to pass to clearInterval. Most
browsers aren't very clever about this, and just returns the value of a
small integer counter. If you don't have any timers yourself (or if you do,
but you know their id's), just create a new timer, and clear that and all
numbers below it (omitting your own timers if necessary).

There are so many other irreversible operations a script can do that you
can't possibly undo a script execution, any more than you can unscramble
eggs.

If you could somehow make the script run in a sandbox, so you can record all
its destructive changes, you might have a chance, but that would be so
incredibly complicated (you need to wrap all builtin functions that can
modify anything, and record the state of all objects, so that you can
restore them to pristine state), that it's bound to be flawed.

/L

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to