loading JavaScript files in run-time

Posted on 2011-09-21
Last Modified: 2012-05-12

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)?
Question by:VapiSoft
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
LVL 15

Expert Comment

ID: 36578520
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
LVL 40

Accepted Solution

gurvinder372 earned 500 total points
ID: 36578531

Author Comment

ID: 36578543
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.

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

LVL 40

Expert Comment

ID: 36578550
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
LVL 75

Expert Comment

by:Michel Plungjan
ID: 36578559
Eval: document.write does not work on demand.

Look at the function posted by Gurvinder to see how it's done
LVL 15

Expert Comment

ID: 36578562
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')


Author Closing Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question