Reputation: 95
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 advance.
P.S.: No framework is being used for this part of the application.
Upvotes: 0
Views: 190
Reputation: 10323
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.
Upvotes: 0
Reputation: 67832
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.
Upvotes: 1