/    Sign up×
Community /Pin to ProfileBookmark

code management

What is the best way to manage javascript code.
1. Use one file and dump everything in it? OR
2. Use multiple files…
any suggestions…..

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@OverstatementJan 04.2007 — It depends:

If you don't have a lot of code, obviously use one file.

If you have a lot of code, split your files into logical groups (like all helper functions in one file).

If you're coding only for yourself, use as many files as you want.

If you're coding and releasing it, try to keep it in one file so the users don't have to put so many <script> tags.
Copy linkTweet thisAlerts:
@mrhooJan 04.2007 — The only advantage that comes with using multiple files is the ability to load them incrementally, or as needed. You could have a single required file loaded in the head of a web page and a dozen others that only get loaded as needed.

You can get a page up quickly and then use the onload event to call for another script. As long as the the code to load more code is in the required script, you're golden. This strategy works best when the page is worth looking at, at least once, for long enough to download the initial scripts.
Copy linkTweet thisAlerts:
@duke_okcauthorJan 05.2007 — Great input...thank you guys. I have lots of code and I guess dividing it to multiple files would suit my needs. Personally I don't like one big file because it gets too much of a hassle to make any change to it or just simply go through it for that matter. Just a quick question, like mrhoo suggested, if I use the onload event to call another script (saved in a separate file), how would it impact the download speed?..is it going to make another trip to server or.....Thanks for all your help.
Copy linkTweet thisAlerts:
@mrhooJan 05.2007 — You do make a new request for every script, but after the first visit it'll be to the cached file. While a script downloads no other script can run, but the user can scroll the page and click links.

The trick is knowing when a particular file has loaded and is ready for use-

ajax here has the advantage, but it makes parsing the scripts more involved.

I give all my scripts a final global method called 'loadcheck_'+ (filename) that returns true, making it simple to use a timer to test for the method(and its script) before I call any of its methods, or load the next file.
×

Success!

Help @duke_okc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...