Skip to content Skip to sidebar Skip to footer

Should A "script" Tag Be Allowed To Remove Itself?

We've been having a discussion at our workplace on this with some for and some against the behavior. Wanted to hear views from you guys :
Tes

Solution 1:

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

Solution 2:

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.

Solution 3:

Actually you can do this:

var currentScript;
currentScript = document.currentScript || document.scripts[document.scripts.length - 1];
currentScript.parentNode.removeChild(currentScript);

Solution 4:

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.

Solution 5:

Yes, it's a valid behavior since script only removes DOM representation of itself. It does not removes itself this way. Actually, script can replace own text representation on the page with something entirely different without being re-run.

Example:

var s = document.createElement('script');
s.textContent = 'alert("Hello, world!");'+
                'document.currentScript.textContent="alert(1)";';
                // BTW, behavior is the same for innerHTMLdocument.body.appendChild(s);

However, there are almost no valid reasons to make script remove itself or change own code unless you want to keep things in the DOM clean. Also, this won't hide your code from anyone willing to look at it. Usually it means you had to add the script from some other code and it's better to use something like document.body.removeChild(s); from that other script anyway.

Post a Comment for "Should A "script" Tag Be Allowed To Remove Itself?"