Link to home
Start Free TrialLog in
Avatar of VapiSoft
VapiSoft

asked on

loading JavaScript files in run-time

Hi,

I have a very big JavaScript application with many JavaScript files.
It takes some time to load especially in new computers (first time the computer uses the app).
Is there something in JavaScript that I can load only the files that I use in the startup,
and then load the rest of the files on a "need to have basis" (when it is called)?
Avatar of Eyal
Eyal
Flag of Israel image

sure you can.
use document.write to include new script files
don't forget to check if the function you want to activate is present before loading the js file
ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of VapiSoft
VapiSoft

ASKER

1. I don't undertand how to do it?
2. Do I need to check every function before I use it? or is there something that can doit automatically?

For example: every time that it encounters an unresolved function (or variable) it will call a special function that will tell the browser what file to download.

no, its just like making an ajax call, since when you make it, you need to go to call back handler to process the response
Eval: document.write does not work on demand.

Look at the function posted by Gurvinder to see how it's done
you can load the files you need with timer after the loading is completed

but it will be a good practice to check if the function exist before using it. let say the file didn't loaded yet

if (typeof functionname == 'function')

The trick at the end of the end of the second article (how to load only one) is great.
Thank you very much.