loading JavaScript files in run-time


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)?
Who is Participating?

Improve company productivity with a Business Account.Sign Up

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
VapiSoftAuthor Commented:
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.

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Gurvinder Pal SinghCommented:
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
Michel PlungjanIT ExpertCommented:
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')

VapiSoftAuthor Commented:
The trick at the end of the end of the second article (how to load only one) is great.
Thank you very much.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.