0

I'm developing a web application that relies on AJAX technology to load the pages. Each page has an associated JavaScript file. When a new page is loaded the previous JavaScript file is removed but the code still remains in the DOM.

What's the best approach to remove the objects of the previous JavaScript file from the DOM?

I've googled about this subject and the right answer appears to be the use of the module pattern but this implies a major refactoring of the project.

Diogo Cardoso
  • 21,637
  • 26
  • 100
  • 138
  • How are you loading the pages (ie where is you code) ? and why is there a javascript file per page ? are you loading in a whole new page ? .... – Manse Mar 02 '12 at 11:18
  • 1
    Unfortunately I think you have chosen the wrong approach for you project. There shouldn't be a reason to load a new JavaScript file for every page - are you sure you can't just load some variables and act accordingly? Why are you loading these JavaScript files in the first place? – MMM Mar 02 '12 at 11:26
  • Look at this: http://stackoverflow.com/questions/9390445/remove-appended-script-javascript/9471916#9471916 – Teemu Mar 02 '12 at 11:51
  • The reason why each page has it's own JavaScript file it's because it's a lot of information and the objects are specific for each page. Why is this a bad approach? – Diogo Cardoso Mar 02 '12 at 11:59
  • @Diogo "*...the previous JavaScript file is removed but the code still remains in the DOM.*" - What do you mean by this? Removed how? Still remains? – Šime Vidas Mar 02 '12 at 13:20
  • @Šime Vidas the – Diogo Cardoso Mar 02 '12 at 14:44
  • @Diogo What objects? Where in the DOM? – Šime Vidas Mar 02 '12 at 15:04
  • @Šime Vidas as I mentioned before each page has it's own JavaScript file associated and the objects I'm refering are the the specific variables and functions of that JavaScript file. That being said, I'm interested in using them only when the page is active, otherwise I want to remove them from the DOM. – Diogo Cardoso Mar 02 '12 at 15:31
  • @Diogo Are those variables and functions global? (Are you polluting the global namespace?) – Šime Vidas Mar 02 '12 at 16:27
  • @Šime Vidas unfortunately yes :( – Diogo Cardoso Mar 02 '12 at 17:17
  • @Diogo Why do you want them removed? If the variable/function names are the same, they will be overwritten by the new script block. – Šime Vidas Mar 02 '12 at 17:33
  • @Šime Vidas the reason why I want to remove the global objects from the DOM it's because there are some variables/functions with the same name in different JavaScript files but also exists objects that are specific of the file. – Diogo Cardoso Mar 03 '12 at 02:38
  • @Diogo Yes, but, as I said, they will be overwritten with the newest versions every time you load a new page. So, for instance, if you have a `print` function on the current page, and you load a new page which also includes a `print` function but with different functionality, then the old version of the function will be replaced by the new version automatically. So, the JavaScript code of the new page is executed, and all new variables/functions are added to the global object, and if there are name collisions, then the old versions will be overwritten be the new versions. – Šime Vidas Mar 03 '12 at 03:14

1 Answers1

0

If you are defining new objects with each script, you can't get rid of them until everything pointing at those objects is gone.

The best approach may be to reuse the same objects- redefine them, rather than creating new ones.

kennebec
  • 102,654
  • 32
  • 106
  • 127