views:

63

answers:

2

Hello people..

I have research about an issue and could not find a solution for it through normal JavaScript techniques (maybe there is but I did not find any).

The thing is, my application loads ( inserts) JavaScript files whenever the user loads a section of the system. The problem comes when I try to unload ( removal) one.

I've noticed that if I load section A (which loads script A'), then load section B (which loads script B'), and then load section A again, the code in script A' does not run again, thus every event registration in there doesn't get performed, which I do want to happen. (Maybe some extra kind of cleaning is required that I am not aware of?)

Any ideas? If what I just said makes any sense...

An example:

1- User enters Section 'Products'

2- Both the UI and JavaScript file associated to 'Products' get loaded

2.1- Products.js is now inserted in the <head>

2.2- Products.js code gets parsed and executed

3- User enters Section 'Clients'

3.1 - Products.js reference is removed from the <head>

4- Both the UI and JavaScript file associated to 'Clients' get loaded

4.1- Clients.js is now inserted in the <head>

4.2- Clients.js code gets parsed and executed

5- User enters Section 'Products'

5.1 - Clients.js reference is removed from the <head>

6- Both the UI and JavaScript file associated to 'Products' get loaded

6.1- Products.js is now inserted in the <head>

6.2- Products.js code **DOES NOT** get parsed nor executed

Thanks in advanced.

P.S.: No framework is being used for this part of the application.

+1  A: 

Not really an answer, but you could rely on an init() method to actually do work, rather than the body of the javascript file. This seems cleaner, at any rate, as re-inserting javascript to cause execution just seems sloppy.

Stefan Kendall
Actually, the application is based on Packages. If they, as you well pointed out, include an init() method, it gets called acting as a constructor for the object represented by that Package, thus having classes in a classless environment.If I understood correctly, you are suggesting that the application runs that "constructor" every time it "loads" a file, right?
leomdg
Yeah. I've seen browsers "load" js when you "insert" script tags "dynamically," although if this is failing, making a sort of "constructor," which "you" rely on anyway, seems sensical.
Stefan Kendall
A sum of this solution and the one chosen in here as preferred answer: http://stackoverflow.com/questions/1704876/re-executing-javascript-files seems to having solved the problem.
leomdg
A: 

YUI provides a special class for loading not only their libraries dynamically from javascript, but your own user defined ones.

Here is the link YUI LOADER

As long as you don't make use of their History manager (which i doubt you would), this should allow you to load and reload javascript files on the fly.

Zoidberg
Thank you Dr. Zoidberg, I mean, Zoidberg, heh.. I know about YUI's tools and as matter of fact I think they have the best framework and various tools in existance.But, as I indicated in the postscript (PS), this part of the application is not using any framework and I don't intend to, but your answer is still valid, since if I would use one, I would go with YUI Loader ;).
leomdg