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
  • 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.

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

806 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