Remove JavaScript's and functions

Discussion in 'Javascript' started by Laser Lips, Oct 19, 2009.

  1. Laser Lips

    Laser Lips Guest

    Hi all.
    I've got a single window application where there are no page loads or
    page changes. All the content is dragged down via ajax including new
    JavaScript code required for what ever is needed for the current
    screen.

    Most screens are broken up into files on the server.

    Each file normally has some JavaScript and some HTML.

    The JavaScript is spliced out and brought to life using the eval
    command. most screens have a 'setUp' function, which is called after
    the content is dowloaded to the page to initialise variables etc. Some
    pages do not have this setUp function but I found that (seems obviouse
    now) is that the JavaScript functions I was bringing down overwrite
    any functions with the same name, and any that are not over written
    are still present in the browser.

    So if a page that get's ajaxed down does not have the setUp function,
    and I call setUp, the setUp function is still trying to work with the
    previous screen, but in fact should not have been called.

    I've tried many things to clear out old functions but all methods have
    proved inconsistant.
    I've tried seperating the JavaScript into their own files and then
    adding adn removing them from the head, but once loaded, even after
    removing them, the functions they contained are still present.

    I've declared functions like this...
    function window.test()
    {

    }
    then to remove them is used a loop like this...
    for(n in window)
    {
    typov="typeof(window."+n+")";
    typov=eval(typov);
    if(typov.toString()=="function")
    {
    eval("window."+n+"=null;");
    }
    }

    This worked quite well but the pages themselves that were ajaxed down
    appeared not to be able to see the functions.

    So my question is how can I load some functions, do what I need to do
    and then remove them afterwards.

    Thanks
    Graham
    Laser Lips, Oct 19, 2009
    #1
    1. Advertising

  2. Laser Lips

    Laser Lips Guest

    So just to clarify, Ive tried the following.

    delete document.getElementById("testjs");
    ....
    document.getElementById("testjs").parentNode.removeChild
    (document.getElementById("testjs"));
    ....
    document.getElementById("testjs").innerHTMl="";
    ....
    document.getElementById("testjs")=null;

    As stated above the only thing that mildly worked was declareing
    functions like this...

    function window.test(){} and then setting it to null later on.
    Laser Lips, Oct 19, 2009
    #2
    1. Advertising

  3. Laser Lips wrote:
    > As stated above the only thing that mildly worked was declareing
    > functions like this...
    >
    > function window.test(){} and then setting it to null later on.


    Not going to work, because of a syntax error in your declaration.

    You can do this:
    window.test = function (){}

    or simply:
    function test(){}

    If you want to remove the function, use:
    delete test;
    John S. Thomsen, Oct 19, 2009
    #3
  4. Laser Lips

    VK Guest

    John S. Thomsen wrote:
    > If you want to remove the function, use:
    > delete test;


    Before giving practical advises it worths to test them first or to
    note them //(untested)
    You cannot use delete operator on Global members, it doesn't error out
    but simply returns false as the indicator that the object cannot
    deleted:
    function f(){}
    window.alert(delete f) // false
    window.alert(f) // the same function
    and you cannot use delete on window object - that simply errors out.

    To OP: you are doing a rather frequent "voodoo doll" cargo cult stuff.
    The <script> elements or AJAX requests are just delivery shells for
    the code. After it successfully parsed on delivery it has no more any
    connection with the shell. So you may remove <script> nodes, swap them
    etc. - it produces the same effect on your code as trampling eggshells
    in the barn would on chickens on the backyard :)

    You don't need am object anymore (that includes functions as well)?
    Then set it to null:
    f = null;
    VK, Oct 19, 2009
    #4
  5. John S. Thomsen wrote:

    > Laser Lips wrote:
    >> As stated above the only thing that mildly worked was declareing
    >> functions like this...
    >>
    >> function window.test(){} and then setting it to null later on.

    >
    > Not going to work, because of a syntax error in your declaration.


    True.

    > You can do this:
    > window.test = function (){}


    But you should not, because `window' is supposed to refer to a host object.
    See below.

    > or simply:
    > function test(){}
    >
    > If you want to remove the function, use:
    > delete test;


    Ignore "Often Wrong" VK; his objection is correct, his explanation
    unsurprisingly is not. Here follows the correct explanation:

    `delete test' does not necessarily work with your first approach because you
    should not assume that `window' refers to the ECMAScript Global Object; if
    you wanted to delete the `test' property of the object that `window' refers
    to, you would need to write `delete window.test' or `delete window["test"]'.

    But again, you should not attempt to add a property named `test' to that
    object to begin with, because it is a host object and does not need to
    support augmentation with properties. As an alternative, you can add
    deletable properties to the Global Object, which is referred to by `this' in
    the global execution context:

    this.test = function() {};

    /* Function */
    test;

    /* or: delete test */
    delete this.test;

    /* ReferenceError */
    test

    (In a local execution context, you would use a property that refers to the
    global object instead of using `this'.)

    `delete test' is also not supposed to delete the property with your second
    approach. Evidently (see above) not because, as VK stated incorrectly, in
    incorrect terminology, it would be "a Global member", but because the
    function declaration causes a property named `test' to be added to the
    Variable Object of the execution context¹ with the attribute `DontDelete'
    (upon variable instiantation, before execution). Therefore, the result of
    `delete test' is `false' (which indicates that the delete operation was
    unsuccessful).

    Be careful with using `test' as property name anyway; for example, using
    that may (and as a function declaration probably will) preclude your code
    from being subject to JSUnit unit tests, where `test' is a built-in.


    PointedEars
    ___________
    ¹ The Variable Object of the global execution context is the Global Object.
    (That is about all in which VK was "correct" here.)
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
    Thomas 'PointedEars' Lahn, Oct 19, 2009
    #5
  6. Laser Lips

    VK Guest

    Thomas 'PointedEars' Lahn wrote:
    >   this.test = function() {};


    Technical equivalent of
    window.test = function() {};

    >   /* or: delete test */
    >   delete this.test;


    I do repeat my previous suggestion: before posting a code sample first
    test it to work or mark it //untested if *really* short on time. This
    chunk of code leads to the same "command not supported" on any version
    of IE including IE8.

    [...]
    VK, Oct 19, 2009
    #6
  7. VK wrote:

    > Thomas 'PointedEars' Lahn wrote:
    >> this.test = function() {};

    >
    > Technical equivalent of
    > window.test = function() {};


    No, as discussed ad nauseam.

    >> /* or: delete test */
    >> delete this.test;

    >
    > [...] This chunk of code leads to the same "command not supported" on any
    > version of IE including IE8.


    It does that in IE 6 at least. Looks as if the JScript guy you are so very
    inspired and impressed by, has failed to implement the Specification
    correctly again.


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
    Thomas 'PointedEars' Lahn, Oct 19, 2009
    #7
  8. Laser Lips

    VK Guest

    > > Thomas 'PointedEars' Lahn wrote:
    > >> this.test = function() {};

    > VK wrote:
    > > Technical equivalent of
    > >  window.test = function() {};

    >
    > No, as discussed ad nauseam.


    I don't know what do you mean by "discussed". Since at least 2006 I
    was stating that window !== Global and never can be and others were
    screaming back that it is "because it is written so in ECMA Books".
    You may call it a discussion if you like. For window vs. Global I
    already provided a link at mozilla.dev.tech.js-engine thread:
    http://groups.google.com/group/mozi..._frm/thread/ef207361cde3c675/5abcdf7cb5c06375
    As you might failed to read the proper parts here is the Boris' post:
    http://groups.google.com/group/mozilla.dev.tech.js-engine/msg/df81825b338fb84f

    Now you may sit and write a long explanatory letter to Brendan Eich
    about how does everything really work. Just please stop posting borken
    code samples.
    VK, Oct 19, 2009
    #8
  9. VK wrote:

    >> > Thomas 'PointedEars' Lahn wrote:
    >> >> this.test = function() {};

    >> VK wrote:
    >> > Technical equivalent of
    >> > window.test = function() {};

    >>
    >> No, as discussed ad nauseam.

    >
    > I don't know what do you mean by "discussed". [rubbish snipped]


    Yes, your reading problem is pretty much evident.


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
    Thomas 'PointedEars' Lahn, Oct 19, 2009
    #9
    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. Simon-Pierre  Jarry
    Replies:
    2
    Views:
    2,352
    Henrik
    Aug 10, 2005
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,577
    Victor Bazarov
    Jun 21, 2004
  3. tshad
    Replies:
    6
    Views:
    21,438
    tshad
    Aug 8, 2006
  4. John Goche
    Replies:
    10
    Views:
    723
    Marcus Kwok
    Dec 8, 2006
  5. An. Valula
    Replies:
    3
    Views:
    170
    Alan J. Flavell
    Oct 23, 2003
Loading...

Share This Page