morgar
morgar

Reputation: 2407

A way of disposing scripts files previously loaded with $.getScript()

Sorry about my English. I'm developing a chess interactive site. There is a lesson menu, when the user selects the lesson, it's loaded using $.getScript(). Lesson file only has a variable lesson that contains a "big" json object. When user selects another lesson, file is loaded and the new lesson var overrides the previous one.

Ok, that works great. But, should I dispose the previous loaded file/s, and if I should, how I do that? What if the user navigates fifty lessons, will it slow down the process?

I took a look in firebug when the file is loaded and the <head>, where supposedly $.getScript() put the file, and it makes a 'yellow flash', but nothing visible is added.

I would want to know your thoughts about this. Thanks in advance.

Upvotes: 2

Views: 509

Answers (2)

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18354

Even if you'd 'dispose' the file, or delete the script tag or whatever, your script already has been exeuted, and therefore, your variables exist.

The only way would be to set to null your json objects when you select another lesson.

For example, if you have the following json in the lesson1.js file:

var lesson1data = { 
  ...Big json comes here...
};

In your main page/script you should have:

function selectLesson(lessonNumber){
   lesson1data = null; //This 'cleans' the loaded data
   lesson2data = null; //This 'cleans' the loaded data
   ...
   $.getScript(...); //Load json data for selected lesson
}

Hope this helps. Cheers

Upvotes: 0

mattsven
mattsven

Reputation: 23283

$.getScript doesn't actually load the file (i.e. appending it to the head as a script tag) but downloads the script .js file and runs it immediately. Of course, you could include some kind of terminating function in your script that wipes itself out when you are done using it.

Upvotes: 1

Related Questions