Application fails to dynamically _re_load JavaScript files

廉价感情. 提交于 2019-12-11 16:36:04

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/1309694/application-fails-to-dynamically-re-load-javascript-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!