2

Basically i want to load JS file like this into the page dynamically

globalVar = "some variable";
alert(globalVar);

And then i want to remove it, i can easily delete script from DOM, but i will have globalVar remain in the memory...

Is there a JS libraries that will help me to load script and then unload it and unload all variables, classes, functions that were declared by that script? Or is there some tricks to do that?

2 Answers 2

3

In JavaScript, when you declare a global variable it is actually a property of the "global" object (usually the DOMWindow, or "window"), so you can simply delete the property by the name of the variable to delete the global reference. For example:

var foo = 123;
alert(foo); // Alerts "123"
alert(window.foo); // Alerts "123"
delete window.foo;
foo; // ReferenceError: foo is not defined.

So if you can keep track of the global variables you use (which should be very, very few, like one) you can just delete it as a property from the "window" or "global" root-level objects.

Update

You can extend this idea to work generally as follows. At the beginning of your script, enumerate the properties of the global object and store them in a reference object. Then, when you want to identify newly added globals, enumerate the global properties again and store only the ones which do not exist in the reference object (meaning they were newly added since you last checked). Then simply delete these additional properties.

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

2 Comments

Well.. as i said.. i loading JS dynamically, co i just don't know what variebles, classed, function it will declare...
And is there a "ready to go" code or framework to do that? ye "I'm lazy as Haskell" :)
0
+50

This is horrible JavaScript. If the JS you're importing doesn't do anything to the global object that is worth keeping around when you remove it (for example changing the DOM), then it shouldn't be writing new variables to the global object at all.

Hopefully you have control over the imported JS, and should encapsulate the code in a self-executing function and ensure that all variable declarations start with var. This means you're not polluting the global object with your own variables and you can do whatever you need to in the script. Your code above would become:

function() {
  var globalVar = "some variable";
  alert(globalVar);
}();

This will ensure that no new variables are written to the global object, and you still have access to them to change them (which presumably you need to do).

If you want more info, could you shine some more light on what the important script actually does?

3 Comments

i think you right at some extend.. i'll mark your 'solution' as an answer for now...
it was probably true at the time you wrote it, but in modern SPA's, asynchronous module loading and unloading is desireable. say you have multiple webpages where you need to instantiate a jquery plugin on a specific element. when you navigate to a new page, the code and event listeners for the previous page still exists.
Thanks for the necro post. Why would you ever unload a module in an SPA yet want to keep the effects of the module around?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.