Removing a loaded script from a page.

Discussion in 'Javascript' started by Daz, Jan 18, 2007.

  1. Daz

    Daz Guest

    Hi everyone.

    Is it possible to remove a script that has been added to a page? 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.

    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. 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?

    Many thanks.

    Daz.
     
    Daz, Jan 18, 2007
    #1
    1. Advertising

  2. Daz

    RobG Guest

    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
     
    RobG, Jan 18, 2007
    #2
    1. Advertising

  3. Daz

    Daz Guest

    RobG wrote:

    > 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


    Simple. Some functions I download would be temporary functions, and
    require deleting at set intervals, or maybe after they are run.
    However, you make a very good point. I'd be better to add temporary
    functions to a temporary namespace, or perhaps manage the scripts
    better, and make a list of namespaces that should be deleted. I had
    thought of this before, but for some reason didn't think it would be as
    'simple' as using iframes as namespaces. Since then, I have had a
    moment of clarity.

    Thanks for your input Rob.

    Daz.
     
    Daz, Jan 19, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marcelo Birnbach [MS]
    Replies:
    0
    Views:
    3,355
    Marcelo Birnbach [MS]
    Jun 28, 2003
  2. Udo Corban
    Replies:
    0
    Views:
    3,909
    Udo Corban
    Jan 23, 2004
  3. Andrew Chalk
    Replies:
    1
    Views:
    321
    Michael Geary
    Aug 28, 2003
  4. Chris  Chiasson
    Replies:
    6
    Views:
    638
    Richard Tobin
    Nov 14, 2006
  5. pbd22
    Replies:
    2
    Views:
    316
Loading...

Share This Page