I have a SCRIPT element with the ID of "dhtml".
The id attribute is not valid for script elements in HTML, however
browsers seem to tolerate it and getElementById works (at least in the
couple of browsers I tested).
I'm dynamically
replacing it with a new script. Before I add the new script I was "told"
to do some garbage collection on the existing script, and was "given"
the following example:
I don't think that was good advice.
var h = document.getElementsByTagName("head")[0];
var d = document.getElementById("dhtml");
See above comment about script elements and the id attribute.
if (d) {
// garbage collection
Javascript garbage collection is not specified to run at any
particular time, nor does a script have any control over it[1] other
than perhaps to create a condition where if it ran, an object might be
removed.
Any object that is no longer referenced by any other property or
variable is a candidate for garbage collection. Therefore the best you
can do is remove all references to an object and hope that the garbage
collector will get rid of it at some time in the future (probably at
the latest when the page is closed, but might not happen until the
browser itself is closed).
For a DOM element, that means at least removing it from the DOM - but
all other references must be removed too (if you've created any).
for (var prop in d) {
try {
delete d[prop];
There is no guarantee that you can delete any property of a host
object.
But even if you can remove them all, it's pointless - it is references
*too* the object that must be cleared. Deleting the object's own
properties has no effect on that.
} catch (err) {
// an error
}
}
h.removeChild(d);
There is no need for a reference to the head element. Assuming d is a
reference to a DOM element, then:
d.parentNode.removeChild(d);
will do the job and doesn't require d's parent node reference to be
hard-coded.
Don't forget to remove your own reference to the element so that it
becomes a candidate for gargage collection:
d = null;
When testing in IE 6 I get the error that 'object doesn't support this
action' even though I've wrapped it in a try/catch.
Which version? Doesn't happen in IE 6 for me - perhaps your script
element isn't in the head.
My questions are:
1. Do I actually need garbage collection when removing a SCRIPT element?
No. Once a script element's content has been parsed and executed, the
objects and properties it creates are no longer influenced by the
script (i.e. the program code). Removing it has no effect on the
current script environment, it just removes a (now virtually useless)
DOM element.
Try this:
window.onload = function() {
var scripts = document.getElementsByTagName('script');
var i = scripts.length;
while (i--) {
scripts
.parentNode.removeChild(scripts);
}
};
2. If it is needed, is there a way to get it to also work in IE 6?
No. The following reference might be helpful:
<URL: http://jrdodds.blogs.com/blog/2006/05/javascript_clos.html >
1. There is a method in JScript to run the garbage collector, but its
use is not recommended.