Remove JavaScript's and functions

L

Laser Lips

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
 
L

Laser Lips

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

John S. Thomsen

Laser said:
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;
 
V

VK

John said:
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;
 
T

Thomas 'PointedEars' Lahn

John said:
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.)
 
V

VK

Thomas said:
  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.

[...]
 
T

Thomas 'PointedEars' Lahn

VK said:
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
 
V

VK

Thomas said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top