+4  A: 

If the question is, "should a script tag be allowed to remove itself?" I would think so. After all, a script tag can cause the browser to navigate to another page, in which case the entire page (including such script tag) is removed from memory.

Will
Well, in the example above, the script tag isn't removed. It still exists, what do you think about that?
Nischal
When you say "it still exists", do you mean it's still a part of the DOM structure? If so, what is the script element's parent after the div element is removed?
Will
@WillInteresting question. Just checked this behavior. If there's only this one 'script' tag then document.getElementsByTagName('script').length gives me a count of 0!But I'm sure the script tag still exists because functions written in that script tag get executed!
Nischal
A: 

The script is removing the DIV from the document DOM tree, and just so happens to remove the script declaration itself (after it has been loaded). So I think it should be allowable as a feature.

Also, the script tag will exist until it is loaded and the Javascript is interpreted and executed. At execution, the DIV (and SCRIPT) tag will be removed from the DOM tree and exist only as objects that will be garbage collected.

EDIT

I think the major point here is that a SCRIPT tag only creates the Javascript objects and places them in memory in the Javascript runtime engine (which is completely different from the DOM). Once the interpreter has finished reading the SCRIPT tag, it's no longer needed and can be discarded (removed from the DOM).

If the interpreter was instructed (by the JS code) to create references to OTHER objects in the DOM (e.g. as the handler for a button click), then the Javascript function will still be accessible. Any functions called from that event handler will ALSO be accessible. In this way, you can "walk" the call stack from the button->handler->other functions/variables.

Jeff Meatball Yang
The script isn't removed in the case above. To confirm this, what you can do is create a javascript function just below the code that deletes the child node. Put a button with onclick of the said function. Keep the button in a new div. The onclick still works, I hope your getting what I mean to say here.
Nischal
That's true Nischal, but what you've created is a reference from the button in the DOM tree to the function that was created in the now-removed SCRIPT tag... which means that the function still exists in memory and won't be garbage collected. See the MSDN article on why IE doesn't handle this properly. http://msdn.microsoft.com/en-us/library/bb250448(VS.85).aspx
Jeff Meatball Yang
A: 

An example where self removal can be handy is, for example, some long polling techniques that would certainly make the DOM large after a few minutes.

JavaScript frameworks add and remove script elements as part of their daily routine.

E.g. getScript() in jQuery places a script tag in the document head and removes it right after evaluation.

They do this to keep the DOM clean - else the tags would just build up unnecessarily. They are no longer needed after evaluation.

The only drawback I see with this is debugging - for example, Firefox with Firebug seems to be lost as to the whereabouts of such scripts and cannot find the source lines. (As of this writing.)

andras