1

I know. It is possible to dynamically load JavaScript and style sheet file into header of document. In the other hand, it is possible to remove script and style sheet tag from header of document. However, loaded JavaScript is still live in memory.

Is it possible to destroy loaded JavaScript from web browser memory? I think. It should be something like the following pseudo code.

// Scan all variables in loaded JavaScript file.
var loadedVariable = getLoadedVariable(JavaScriptFile);

for(var variable in loadedVariable)
{
    variable = null;
}

// Do same thing with function.

Is it possible to create some JavaScript for doing like this?

Thanks,

PS. Now, you can use xLazyLoader and jQuery for dynamic loading content.

2
  • Why do you think you need to? Commented Oct 22, 2009 at 9:03
  • Because I want to create some web application that has not any iframe. Every page will be loaded via Ajax. Moreover, JavaScript and style sheet will be loaded depending on page requirement. After that, engine will destroy unused JavaScript and style sheet for reducing duplicate name of function, variable, rule of JavaScript and style sheet. Commented Oct 22, 2009 at 9:09

2 Answers 2

3

If the loaded script is assigned to a window property, for instance with the module pattern like so:

window.NiftyThing = (function() {
    function doSomething() { ... }
    return {
        doSomething: doSomething
    };
})();

or

window.NiftyThing = {
    doSomething: function() { ... }
};

or

NiftyThing = {
    doSomething: function() { ... }
};

Then you can delete the property that references it:

delete window.NiftyThing;

...which removes at least that one main reference to it; if there are other references to it, it may not get cleaned up.

If the var keyword has been used:

var NiftyThing = {
    doSomething: function() { ... }
};

...then it's not a property and you can't use delete, so setting to undefined or null will break the reference:

NiftyThing = undefined;

You can hedge your bets:

NiftyThing = undefined;
try { delete NiftyThing; } catch (e) { }

In all cases, it's up to the JavaScript implementation to determine that there are no outstanding external references to the loaded script and clean up, but at least you're giving it the opportunity.

If, as Guffa says, the loaded script doesn't use the module pattern, then you need to apply these rules to all of its symbols. Which is yet another reason why the module pattern is a Good Thing(tm). ;-)

Sign up to request clarification or add additional context in comments.

2 Comments

Therefore, I need to use regular expression for finding every possible pattern of variable and function. Right?
If you're trying to do it without knowing what it is you're unloading, I think you have a lot of work ahead of you. And you won't know that it will even work; the script could have set event handlers, which will keep references. You could end up just breaking it (event handler trying to call a function via a symbol you've removed) rather than removing it.
1

It might be possible to remove a Javascript file that has been loaded, but that doesn't undo what the code has done, i.e. the functions that was in the code are still defined.

You can remove a function definition by simply replacing it with something else:

myFunction = null;

This doesn't remove the identifier, but it's not a function any more.

1 Comment

Can I remove it by setting as undefined value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.